Here's a very simple example; the call to Wait
releases the lock (allowing Worker
to obtain it) and adds the Main
thread to the lock-object's pending queue. Worker
then obtains the lock, and calls Pulse
: this moves the Main
thread into the lock-object's ready queue. When Worker
releases the lock, Main
can resume work.
Note that lock(obj) {...}
is just compiler-candy for Monitor.Enter
/Monitor.Exit
in a try/finally block.
[edit: I changed the sample to move lock(sync)
earlier, to avoid the (unlikely) risk of a missed Pulse]
static void Main()
{
object sync = new object();
lock (sync)
{
ThreadPool.QueueUserWorkItem(Worker, sync);
Console.WriteLine("Main sleeping");
// wait for the worker to tell us it is ready
Monitor.Wait(sync);
Console.WriteLine("Main woke up!");
}
Console.WriteLine("Press any key...");
Console.ReadKey();
}
static void Worker(object sync)
{
Console.WriteLine("Worker started; about to sleep");
Thread.Sleep(5000);
Console.WriteLine("Worker about pulse");
lock (sync)
{ // notify Main that we did something interesting
Monitor.Pulse(sync);
Console.WriteLine("Worker pulsed; about to release lock");
}
Console.WriteLine("Worker all done");
}