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

Yan Cui

I help clients go faster for less using serverless technologies.

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


Whenever you’re ready, here are 3 ways I can help you:

  1. Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game.
  2. Consulting: If you want to improve feature velocity, reduce costs, and make your systems more scalable, secure, and resilient, then let’s work together and make it happen.
  3. Join my FREE Community on Skool, where you can ask for help, share your success stories and hang out with me and other like-minded people without all the negativity from social media.

 

Leave a Comment

Your email address will not be published. Required fields are marked *