sync_primitives v0.2.0 SyncPrimitives.CyclicBarrier View Source
A CyclicBarrier expects a predefined number of parties
to await/2
before all calls to await/2
can continue.
Parties arrive at the barrier by calling await/2
.
When all parties
have arrived, all calls to await/2
unblock, and parties may proceed.
Thereafter, the barrier resets (which is what makes it cyclic).
Although this fully describes the happy path, documentation for time outs, participating parties that exit, and other sad paths is currently lacking.
Example
iex> barrier = SyncPrimitives.CyclicBarrier.start(2, fn -> IO.puts("barrier action") end)
{SyncPrimitives.CyclicBarrier, #PID<0.149.0>}
iex> spawn_link(fn ->
...> IO.puts("process 1, before wait")
...> SyncPrimitives.CyclicBarrier.await(barrier)
...> IO.puts("process 1, after wait")
...> end)
process 1, before wait
#PID<0.155.0>
iex> spawn_link(fn ->
...> IO.puts("process 2, before wait")
...> SyncPrimitives.CyclicBarrier.await(barrier)
...> IO.puts("process 2, after wait")
...> end)
process 2, before wait
#PID<0.161.0>
barrier action
process 1, after wait
process 2, after wait
iex> SyncPrimitives.CyclicBarrier.stop(barrier)
Link to this section Summary
Functions
Returns true
, if any of the parties waiting for the barrier timed out or,
exited since construction or the last reset, false
otherwise.
Returns the number of parties currently waiting for the barrier.
Returns the number of parties required to trip this barrier.
Link to this section Types
Link to this section Functions
Returns true
, if any of the parties waiting for the barrier timed out or,
exited since construction or the last reset, false
otherwise.
Returns the number of parties currently waiting for the barrier.
Returns the number of parties required to trip this barrier.
Resets the barrier to its initial state. If any parties are currently waiting
at the barrier, the await/1
or await/2
calls will return :broken
.
start(parties, action \\ nil)
View Sourcestart(pos_integer(), nil | (() -> any())) :: barrier()
Starts a new CyclicBarrier that expects parties
processes to call await/1
or await/2
before it releases. Calls to await/1
block until all expected
parties have called await/1
. Thereafter, the barrier resets (which is what
makes it cyclic).