Module sync/channel
Bounded multi-producer multi-consumer (MPMC) channel. Uses blocking send/recv via condition variables — does not require IO.
Channel uses atomic object with atomic reference counting, so it can be
safely shared across threads without Arc wrapping.
Example
{ Channel } :: import "std/sync/channel";
{ Thread } :: import "std/thread";
ch := Channel(i32).new(usize(10));
t := Thread.spawn(() => {
ch.send(i32(42));
});
val := ch.recv(); // blocks until data available
assert((val.unwrap() == i32(42)), "received value");
t.join();
Types
Thread-safe bounded channel for passing values between threads.
Uses atomic reference counting — implements Send and can be
safely shared across threads without Arc wrapping.
T must implement Send to ensure values are safe to transfer between threads.
Type Parameters
| Name | Type | Notes |
|---|---|---|
T | Type | comptime |
Trait Implementations
impl(forall(T : Type), where(T <: Send), Channel(T), ...)
new : (fn(capacity: usize) -> Self)Create a bounded channel with the given capacity. Capacity must be > 0.
Returns: Self
send : (fn(self: Self, value: T) -> Result(unit, unit))Send a value into the channel. Blocks if the channel is full until space becomes available. Returns .Ok(()) on success, .Err(()) if the channel is closed.
Returns: Result(unit, unit)
recv : (fn(self: Self) -> ?T)Receive a value from the channel. Blocks if the channel is empty until a value is available. Returns .Some(value) on success, .None if the channel is closed and empty.
Returns: ?T
try_send : (fn(self: Self, value: T) -> Result(unit, unit))Try to send a value without blocking. Returns .Ok(()) if sent, .Err(()) if full or closed.
Returns: Result(unit, unit)
try_recv : (fn(self: Self) -> ?T)Try to receive a value without blocking. Returns .Some(value) if available, .None if empty.
Returns: ?T
close : (fn(self: Self) -> unit)Close the channel. No more values can be sent. Pending recv() calls will return .None once the buffer is drained. Pending send() calls will return .Err(()).
Returns: unit
is_closed : (fn(self: Self) -> bool)Check if the channel is closed.
Returns: bool
len : (fn(self: Self) -> usize)Return the number of values currently buffered.
Returns: usize
is_empty : (fn(self: Self) -> bool)Check if the channel buffer is empty.
Returns: bool
impl(forall(T : Type), where(T <: Send), Channel(T), Dispose(...))
dispose : (fn(self: Self) -> unit)Returns: unit