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

Paulo Sérgio Almeida psa@REDACTED
Wed Oct 1 15:19:52 CEST 2008

Joel Reymont wrote:
> On Oct 1, 2008, at 11:38 AM, Richard Carlsson wrote:
>> How exact do you need the synchronization to be? The simplest barrier
>> is a receive that waits for a start message
> That's what I was thinking. The question then is how to ensure that  
> all processes get the start message more or less at the same time. I  
> guess this is the question of how long it takes to send 10k-50k start  
> messages.

Here is a version using links and trap_exit, intending to "send" the 
messages atomically, before being preempted. Does anyone know if the 
propagation of EXITs upon termination is atomic (at least within the 
same node) or it can be preempted and resumed later?


-export([init/0, wait/1, go/1]).

init() -> spawn(fun() -> receive go -> ok end end).

wait(Barrier) ->
   TE = process_flag(trap_exit, true),
   receive {'EXIT', Barrier, normal} -> ok end,
   process_flag(trap_exit, TE).

go(Barrier) -> Barrier ! go.

More information about the erlang-questions mailing list