Lock and Monitor
Learn about the most common ways to synchronize threads.
We'll cover the following...
Introduction
The basic principles behind different thread synchronization mechanisms are almost the same. Whenever a thread will use a shared resource (and thus enter the critical section), it locks it. If two threads are to use the same shared resource, one of them waits while the other one works with it.
The lock
keyword
The most straightforward way to achieve our synchronization objectives is to enclose the critical section of our code within a lock
block. We have to use some dummy object as a locker:
static object locker = new
...