[erlang-questions] best way to implement a "barrier"

Richard Carlsson richardc@REDACTED
Wed Oct 1 12:38:52 CEST 2008


Joel Reymont wrote:
> I need to make sure that a bunch of processes start doing work all at  
> the same time. I'm looking at 10-50k processes. What is the best way  
> to implement this?
> 
> To be more precise, I need to make sure my poker games hang around  
> until told to start. What happens otherwise is that I try to start,  
> say, 10k games and by the time games 7-10k start, the first few  
> thousand may have already finished. This complicates counting the  
> number of games running concurrently.

How exact do you need the synchronization to be? The simplest barrier
is a receive that waits for a start message. But if that is not exact
enough (some have finished before others have even woken up), you might
try a clock synchronization (unless that will overload the Erlang timer
wheel): tell each process to go into a state waiting for a specific
time (expressed as a datetime). Each process will then individually
calculate how many milliseconds that is from now, and go into a simple
'receive after N -> ok end' wait. Possibly, this will wake them all up
more simultaneously than the message-based method. The set time must
be sufficiently far ahead that all processes have had time to react
and enter the wait state.

    /Richard



More information about the erlang-questions mailing list