multithreadingdelphisynchronizationdelphi-2009tmonitor

In Delphi, what does System.TMonitor.Pulse and TMonitor.PulseAll actually do


I was very pleased to see Delphi introduce the TMonitor record in Delphi 2009, permitting you to lock specific objects in a multithreaded environment. What has puzzled me is the Pulse and PulseAll methods of this record type.

For example, the entry for Pulse in Delphi's help states "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."

Really? What does that mean? I have used TMonitor without using Pulse without issues. In addition, some of the uses of TMonitor in Delphi's source never use Pulse.

Are the Pulse and PulseAll methods only included in Delphi's TMonitor record for source-level compatibility with the .NET Monitor class, or do they really serve a purpose?

There are two questions ("TMonitor.Pulse vs TMonitor.PulseAll" and "What is TMonitor in Delphi System unit good for") that speak to this issue, but I am looking for a definitive answer.


Solution

  • The wikipedia link in my answer to the referenced question provides a discussion of the use of the wait/pulse/pulseall functionality of a monitor. A thread must enter the monitor them call wait. Then another thread must enter the monitor and call pulse or pulseall to signal the first waiting thread. Pulse only signals one waiting thread, whereas pulseall signals all waiting threads. Look at the condition variable section of the wikipedia article for a more detailed discussion. There are also comments in the RTL source describing the methods of TMonitor.