Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Please can someone give me a simple example of how to use System.Monitor in C#?

I find System.Monitor very confusing, although I understand threading, locks, deadlocks, race conditions, dining philosophers and all that jazz. Normally I use a ManualResetEvent() to do inter-thread co-ordination, but I know that that's a heavyweight kernel object, and that System.Monitor (Enter/Pulse, etc.) is much more efficient. I've Googled and Googled but cannot find a sensible example.

I would be most grateful if the SO crew could explain this potentially wonderful construct to me :-)

like image 924
endian Avatar asked Dec 22 '22 14:12

endian


1 Answers

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");
    }
like image 194
Marc Gravell Avatar answered Mar 30 '23 01:03

Marc Gravell