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

Ever won­dered when you should use Monitor.Pulse and when you should use Monitor.PulseAll giv­en that only one thread will be able to acquire the lock even if you wake up mul­ti­ple threads? I did, and that’s when I stum­bled across a sim­i­lar ques­tion on Stack­Over­flow and as ever, Jon Skeet came up with a real­ly good anal­o­gy for when either should be used:

Imag­ine you’ve got a sin­gle print­er. Only one per­son can use it at a time, so if you’re got a lot of peo­ple wait­ing, you send them all to sleep — but you only wake one per­son up when the print­er becomes free. This mir­rors the use of Pulse.

Now imag­ine you run a shop. While you’re closed, cus­tomers wait out­side the shop. When you open the shop, you don’t just want to wake up one cus­tomer — they can all come in now. This mir­rors the use of PulseAll.


Stack­Over­flow ques­tion on the dif­fer­ence between Pulse and PulseAll

Liked this post? Why not support me on Patreon and help me get rid of the ads!