Why sleep (Process.sleep or :timer.sleep) freed process messages?

Yeah if the main process doesn’t sleep longer than the workers I think you’re right

To be honest, in vanilla OTP25 implementation this example should work correctly in all situations and all environments. But I agree that this is not the behavior you want to rely on

Yes, back in the old days before multicore when there was only one scheduler we had very strict requirements that the system should NEVER block. So it is as @ChrisYammine says the scheduler forces processes to yield control, either after a fixed number of reductions (function calls), waiting for a message or timeout and other things. This is why you have never had to think about making sure a process doesn’t block the system. It can’t.

This of course just works in the same way with many schedulers.

There is a lot, A LOT, of effort put into the BEAM implementation to make this work efficiently and painlessly.

8 Likes