Module channels

Group by:

Channel support for threads. Note: This is part of the system module. Do not import it directly. To activate thread support you need to compile with the --threads:on command line switch.

Note: The current implementation of message passing is slow and does not work with cyclic data structures.


Channel* {.
.}[TMsg] = RawChannel
a channel for thread communication   Source Edit


proc send*[TMsg](c: var Channel[TMsg]; msg: TMsg)
sends a message to a thread. msg is deeply copied.   Source Edit
proc recv*[TMsg](c: var Channel[TMsg]): TMsg
receives a message from the channel c. This blocks until a message has arrived! You may use peek to avoid the blocking.   Source Edit
proc tryRecv*[TMsg](c: var Channel[TMsg]): tuple[dataAvailable: bool, msg: TMsg]
Tries to receive a message from the channel c, but this can fail for all sort of reasons, including contention. If it fails, it returns (false, default(msg)) otherwise it returns (true, msg).   Source Edit
proc peek*[TMsg](c: var Channel[TMsg]): int
returns the current number of messages in the channel c. Returns -1 if the channel has been closed. Note: This is dangerous to use as it encourages races. It's much better to use tryRecv instead.   Source Edit
proc open*[TMsg](c: var Channel[TMsg])
opens a channel c for inter thread communication.   Source Edit
proc close*[TMsg](c: var Channel[TMsg])
closes a channel c and frees its associated resources.   Source Edit
proc ready*[TMsg](c: var Channel[TMsg]): bool
returns true iff some thread is waiting on the channel c for new messages.   Source Edit