sync/ContextGroup

Group handle for spawned contexts with shared waiting and cancellation. A ContextGroup manages completion for multiple contexts, returns no per-context handle, and exposes no per-context outputs.


Group and completion model


Methods

INIT (--) Creates an empty group.
spawn (callable --) Schedules one callable as a new member of the group. The callable returns no value.
wait (--) Waits until the group becomes empty.

Remarks

  • Calling wait on an empty group returns immediately.
cancel (--) Requests cancellation of every current member and returns immediately.

Remarks

  • Only members that are current when cancel is called receive that request.

Waiting and cancellation semantics


Examples

Wait on an empty group

"sync/ContextGroup" use
"String" use
"control" use

{} Int32 {} [
  group: ContextGroup;
  @group.wait
  ("done" LF) printList
  0
] "main" exportFunction

Expected Output

done

Spawn two contexts and wait for the group

"sync/ContextGroup" use
"String" use
"control" use

{} Int32 {} [
  group: ContextGroup;
  [("one" LF) printList] @group.spawn
  [("two" LF) printList] @group.spawn
  @group.wait
  ("done" LF) printList
  0
] "main" exportFunction

Expected Output

one
two
done

Cancel current members

"sync/ContextGroup" use
"sync/sync" use
"String" use
"control" use

{} Int32 {} [
  group: ContextGroup;
  [10.0r64 sleepFor canceled? [
    ("canceled" LF) printList
  ] [
    ("not-canceled" LF) printList
  ] if] @group.spawn
  [10.0r64 sleepFor canceled? [
    ("canceled" LF) printList
  ] [
    ("not-canceled" LF) printList
  ] if] @group.spawn
  @group.cancel
  @group.wait
  ("done" LF) printList
  0
] "main" exportFunction

Expected Output

canceled
canceled
done

See also