Concurrency Domain
The Concurrency domain unlocks the power of doing multiple things at once.
It provides Fibers (lightweight threads) and Channels for safe communication. Whether you're fetching two APIs in parallel or building a background worker, this domain gives you the high-level tools (par, scope) to write concurrent code that doesn't melt your brain.
aivi
use aivi.concurrency as concurrentTypes
aivi
type Scope = Unit
type ChannelError = ClosedFunctions
| Function | Explanation |
|---|---|
par left right | Runs both effects concurrently and returns both results; fails if either fails. |
scope run | Creates a structured concurrency scope and runs run (current Scope is Unit). |
Channels
Channels provide a mechanism for synchronization and communication between concurrent fibers.
make
| Function | Explanation |
|---|---|
make sample | Creates a new channel and returns (Sender, Receiver). |
send
| Function | Explanation |
|---|---|
send sender value | Sends value to the channel; may block if buffered and full or no receiver is ready. |
recv
| Function | Explanation |
|---|---|
recv receiver | Waits for the next value; returns Ok value or Err Closed. |
close
| Function | Explanation |
|---|---|
close sender | Closes the channel from the sender side; receivers observe Err Closed. |