简体   繁体   中英

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 :-)

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");
    }

See if this part of my threading article helps... (the second half of that page). It implements a producer/consumer queue: when the producer produces something in the queue (and finds it was empty - as an optimisation), it pulses the monitor to wake up any waiting threads. When a consumer tries to consume from the queue but finds it empty, it waits for a pulse before trying again.

Alternatively, Joe Albahari's threading tutorial has a section on Wait/Pulse too.

It's quite similar to the WaitHandle stuff you're used to - although frankly I find it easier to get my head round than WaitHandles, mostly because it's so similar to the Java wait/notify that I "grew up" with :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM