Note that there are some explanatory texts on larger screens.

plurals
  1. POTMonitor.Pulse vs TMonitor.PulseAll
    primarykey
    data
    text
    <p>The Delphi Docwiki explains that <a href="http://docwiki.embarcadero.com/VCL/en/System.TMonitor.Pulse" rel="nofollow noreferrer">Pulse</a> notifies the next thread in the waiting queue that it will be able to lock the specified object as soon as the calling thread releases the object. <a href="http://docwiki.embarcadero.com/VCL/en/System.TMonitor.PulseAll" rel="nofollow noreferrer">PulseAll</a> signals all threads in the waiting queue.</p> <p>I <a href="http://delphihaven.wordpress.com/2011/05/06/using-tmonitor-2/" rel="nofollow noreferrer">found</a> this code which uses Pulse in a threaded queue implementation, and given the definition above, think that PulseAll should be used - or asked in a different way: when is it correct to use Pulse instead of PulseAll? (Where the basic question is: how can I be sure that the 'next thread in the queue' is alyways the thread who needs to be notified, except in the trivial cases when there are only two threads in total, or the code can safely assume that the only waiting thread is the thread who needs to be nofified/'pulsed')?</p> <pre><code>function TSimpleThreadedQueue.Enqueue(const Item: T; Timeout: LongWord): TWaitResult; begin if Closed then Exit(wrAbandoned); if not TMonitor.Enter(FQueue, Timeout) then Exit(wrTimeout); try if Closed then Exit(wrAbandoned); FQueue.Enqueue(Item); TMonitor.Pulse(FQueue); Result := wrSignaled; finally TMonitor.Exit(FQueue); end; end; </code></pre> <p>For the corresponding synchronization methods in the Java language I found this question: <a href="https://stackoverflow.com/questions/37026/java-notify-vs-notifyall-all-over-again">Java: notify() vs. notifyAll() all over again</a></p> <hr> <p>Update: the Java question linked above has one interesting answer which shows how a deadlock can occur even in a producer/consumer application which has only two methods, put and get, and which uses notify() (the Java counterpart of Pulse()): <a href="https://stackoverflow.com/questions/37026/java-notify-vs-notifyall-all-over-again/3186336#3186336">Java: notify() vs. notifyAll() all over again</a></p> <p>The answer contains the recommendation</p> <blockquote> <p>If you are not sure which to use, then use <code>notifyAll</code>.</p> </blockquote>
    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.
 

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