Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>According to <a href="http://msdn.microsoft.com/en-us/library/ateab679.aspx" rel="noreferrer">MSDN: Monitor.Wait Method(Object)</a> </p> <blockquote> <p>SynchronizationLockException: The calling thread does not own the lock for the specified object.</p> </blockquote> <p>In other words: You can only call <code>Monitor.Wait(Object)</code>, when you <strong>already own</strong> the lock, whereas you call <code>Monitor.Enter(Object)</code> in order to <strong>acquire</strong> the lock.</p> <p>As for why <code>Monitor.Wait</code> is needed: If your thread realizes, that it is lacking information to continue execution (e.g. it's waiting for a signal), you might want to let other threads enter the critical section, because not all threads have the same prerequisites.</p> <p>For the waiting thread to continue execution, you will need to call <code>Monitor.Pulse(Object)</code> or <code>Monitor.PulseAll(Object)</code> <strong>before</strong> releasing the lock (otherwise, you're going to get the same kind of exception as with <code>Monitor.Wait(Object)</code>).</p> <p>Keep in mind, that the next thread that acquires the lock after a pulse and after the lock was released, is not necessarily the thread that received the pulse.</p> <p>Also keep in mind, that receiving a pulse, is not equivalent to having your condition met. You might still need to wait just a little longer:</p> <pre><code>// make sure to synchronize this correctly ;) while (ConditionNotMet) { Monitor.Wait(mutex); if (ConditionNotMet) // We woke up, but our condition is still not met Monitor.Pulse(mutex); // Perhaps another waiting thread wants to wake up? } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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