Threading – when to use Monitor.Pulse and Monitor.PulseAll

Ever wondered when you should use Monitor.Pulse and when you should use Monitor.PulseAll given that only one thread will be able to acquire the lock even if you wake up multiple threads? I did, and that’s when I stumbled across a similar question on StackOverflow and as ever, Jon Skeet came up with a really good analogy for when either should be used:

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.

References:

StackOverflow question on the difference between Pulse and PulseAll