Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I believe you're confusing windows locks (mutex, critical sections) with .Net monitors (which in C# can use the keyword lock as syntax sugar for Enter/Exit a block.)</p> <p>Windows mutexes and critical sections can only be entered and exited, i.e. there is only one queue associated with them. All threads in the queue wait for the lock to be released and when that happens the next thread on the queue takes control of the lock and starts running. All this happens automatically. The thread currently holding the lock has no part in this, it cannot choose to wake one or more of the other threads waiting for the lock to be released.</p> <p>.Net's Monitor has two queues: a ready queue and a wait queue.</p> <p>The ready queue behaves exactly like a Win32 Mutex or Critical Section queue and is controlled using the Enter/Leave methods.</p> <p>The wait queue is a separate queue controlled using the Wait/Pulse/PulseAll methods. These methods can only be called by the thread holding the Monitor. When a thread calls Wait it releases the Monitor and enters the wait queue. A different thread can then call either Pulse to move one thread or Pulse All to move all threads from the wait to the ready queue (remember the thread calling Pulse/PulseAll holds the Monitor.)</p> <p>From a computer science point of view the Monitor is the single primitive needed synchronize threads (semaphores, events, mutexes, barriers, etc. can all be implemented with Monitors), from a practical point of view Monitors are useful as mutexes and for situations when actions need to be performed by two threads in lock-step. Most of the time however the code is more readable when using events.</p> <p>Further reading:</p> <p><a href="http://en.wikipedia.org/wiki/Monitor_%28synchronization%29" rel="nofollow">Wikipedia page about Monitors for the historic/computer science aspect</a></p> <p><a href="http://msdn.microsoft.com/en-AU/library/system.threading.monitor.aspx" rel="nofollow">MSDN Monitor class</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload