Picos_std_structured.Flock
An implicit dynamic flock of fibers guaranteed to be joined at the end.
Flocks allow you to conveniently structure or delimit concurrency into nested scopes. After a flock returns or raises an exception, no fibers forked to the flock remain.
An unhandled exception, or error, within any fiber of the flock causes all of the fibers forked to the flock to be canceled and the flock to raise the error exception or error exceptions raised by all of the fibers forked into the flock.
ℹ️ This is essentially a very thin convenience wrapper for an implicitly propagated Bundle
.
⚠️ All of the operations in this module, except join_after
, raise the Invalid_argument
exception in case they are called from outside of the dynamic multifiber scope of a flock established by calling join_after
.
join_after scope
creates a new flock for fibers, calls scope
after setting current flock to the new flock, and restores the previous flock, if any after scope
exits. The flock will be implicitly propagated to all fibers forked into the flock. A call of join_after
returns or raises only after scope
has returned or raised and all forked fibers have terminated. If scope
raises an exception, error
will be called.
The optional on_return
argument specifies what to do when the scope returns normally. It defaults to `Wait
, which means to just wait for all the fibers to terminate on their own. When explicitly specified as ~on_return:`Terminate
, then terminate ?callstack
will be called on return. This can be convenient, for example, when dealing with daemon fibers.
terminate ()
cancels all of the forked fibers using the Terminate
exception. After terminate
has been called, no new fibers can be forked to the current flock.
The optional callstack
argument specifies the number of callstack entries to capture with the Terminate
exception. The default is 0
.
ℹ️ Calling terminate
at the end of a flock can be a convenient way to cancel any background fibers started by the flock.
ℹ️ Calling terminate
does not raise the Terminate
exception, but blocking operations after terminate
will raise the exception to propagate cancelation unless propagation of cancelation is forbidden.
terminate_after ~seconds ()
arranges to terminate
the current flock after the specified timeout in seconds
.
val fork_as_promise : (unit -> 'a) -> 'a Promise.t
fork action
is equivalent to fork_as_promise action |> ignore
.