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

1

11

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.

Tutelage answered 29/8, 2011 at 1:33 Comment(0)
H
4

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.

Hargeisa answered 29/8, 2011 at 2:6 Comment(5)
@johan Apparently Wait and Pulse don't work properly. Lots of blogging about it a couple of months back from Chris Rolliston, Primoz, Eric Grange etc.Novelette
Allen talked about a hotfix at the time it was discussed, and I believe he is in a position to follow it through.Villalba
@Johan, here is a link how to reproduce the flaw in TMonitor, #4856806Villalba
Thanks, Allen. If I read your comments correctly, Pulse and PulseAll only apply when a thread that has locked an object calls Wait to temporarily release the lock to another thread. As for a thread attempting to obtain a lock using Enter or TryEnter, Pulse and PulseAll do not apply. To make matters worse, in a test app I locked an object, tried to enter that lock with another thread, called Wait on the first thread at which time the waiting thread took possession. The second thread then released that lock, at which time the original thread regained the lock. No calls to Pulse were used.Tutelage
@Cary, if you have a test case, I'd like to see it. That doesn't sound right. Wait should block until Pulse or PulseAll is called from another thread.Hargeisa

© 2022 - 2024 — McMap. All rights reserved.