28

Monitor.PulseAll notifies all waiting threads in the queue.

Monitor.Pulse notifies a thread in the waiting queue. (The next waiting thread)

Only the next thread (one thread) can acquire the lock. So what is the difference?

When should I use Pulse vs PulseAll?

2
  • Smart remark: Pulse causes one problem, PulseAll causes all sorts of problems. You should really use ManualResetEvent or AutoResetEvent. Oct 4, 2011 at 14:29
  • @JonathanDickinson, can you elaborate on "problem" and "should" part?
    – Sinatr
    Aug 13, 2021 at 8:37

2 Answers 2

47

Use PulseAll when you want to wake up multiple threads, because the condition they're waiting for may now be fulfilled for more than one thread. (Waiting is almost always associated with a condition - you should usually be testing that condition in a while loop.)

Use Pulse when you only want to wake up one thread, because only one thread will actually be able to do useful work.

To give two analogies:

Imagine you've got a single printer. Only one person can use it at a time, so if you're got a lot of people waiting, you send them all to sleep - but you only wake one person up when the printer becomes free. This mirrors the use of Pulse.

Now imagine you run a shop. While you're closed, customers wait outside the shop. When you open the shop, you don't just want to wake up one customer - they can all come in now. This mirrors the use of PulseAll.

5
  • 3
    "When the thread that invoked PulseAll releases the lock, the next thread in the ready queue acquires the lock." - if i can only handle one customer a time - why should i wake up everybody? Mar 23, 2009 at 21:48
  • If you can only handle one customer at a time, you wouldn't. Unless you wanted to let them all browse, and then queue at the checkout to pay...
    – Jon Skeet
    Mar 23, 2009 at 21:50
  • (Very few shops only allow one customer in at a time :)
    – Jon Skeet
    Mar 23, 2009 at 21:51
  • i think now i understand. if i have more worker threads in state "idle" i would use PulseAll to wake up more jobs... (o; thanks! Mar 23, 2009 at 21:53
  • 1
    @JonSkeet: What if you can only handle one customer at a time, but might not always be able to handle some customers [e.g. all customers are waiting because the store is empty; a shipment comes in that contains something many customers would like, but it's possible that the first two might not be interested]. If each of the first two customers, on exiting the Wait, checks if it's interested in what's for sale and if not calls Pulse and Wait, is there any guarantee that anyone other than the first two customers will ever get served?
    – supercat
    May 9, 2013 at 17:35
40

A Monitor has two queues: the waiting queue and the ready queue. In the absence of Wait and Pulse, all threads that try to acquire the lock go into the ready queue. When the lock becomes available, one thread from the ready queue will acquire it.

When a thread acquires the lock and then does a Wait, that thread goes into the waiting queue. It's waiting for a Pulse or PulseAll, and will remain in the waiting queue until it receives a Pulse, even if other threads from the ready queue acquire and release the lock.

Pulse moves one thread from the waiting queue to the ready queue. PulseAll moves ALL threads from the waiting queue to the ready queue.

The key here is that threads in the waiting queue can never acquire the lock. They are waiting for a pulse to move them back to the ready queue so that they can acquire the lock when it becomes available.

There's a reasonably good discussion of Wait and Pulse--at least a bit to get you started--here.

2
  • 1
    Thank you, very well explained! Can you please clarify what happens to the thread that pulses? Does it move to the ready queue and can be scheduled again immediately instead of the thread that just moved from the waiting queue to the ready queue (because of the Pulse)? Jul 18, 2015 at 13:44
  • 5
    @RamiYampolsky: The thread that does the Pulse retains the lock. It must call Wait or Exit to release the lock before the next thread in the ready queue can obtain the lock. Jul 19, 2015 at 4:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.