chickadee » gochan


Go-inspired channels for Chicken Scheme. Essentially thread-safe fifo queues that are useful in concurrency and for thread synchronization. This implementation has largely been inspired by this Go channel tutorial.

chicken-gochan has no egg dependencies.

Development Status

Currently supported:

Comparison to real Go Channels


(gochan [item1] [item2] ...)procedure

Construct a gochan channel initialized with optional items. Each gochan holds a buffer whose length is limited by memory only.

gochan-send chan msgprocedure

Add msg to chan's message buffer. This will "awaken" a single thread, if any, waiting for messages on chan. It is an error to send to a closed channel.

gochan-receive chans #!optional timeout/secondsprocedure

chans is a single gochan or a list of gochans. Pops next msg from any of chans' buffers in a thread-safe manner, if possible. Otherwise, if channel is empty, wait for a gochan-send from another thread. If timeout is supplied and is reached, or all channels are empty and closed, an error is thrown.

If multiple channels in chans have messages available, the message from the first channel is popped first.

If you have multiple threads waiting messages from the same gochan, the order in which threads receive messages is unspecified (but each message is received only once).

gochan-receive* chans timeout/secondsprocedure

Like gochan-receive, but doesn't error out. Instead, it returns: #f for all channels closed, #t for timeout and (list <msg>) on success (distinguishable from a #f or #t message).

gochan-close chanprocedure

Close channel. It is an error to send to a closed channel. It is an error to receive from an empty and closed channel.

gochan-closed? chanprocedure

Closed predicate.

gochan-for-each chans procprocedure

Call (proc <msg>) for each msg in chan (proc must unfortunately have side-effects). Returns (void) when chan is closed.

gochan-fold chans procprocedure

Like a normal fold, but fold over chans's messages.

(gochan-select (<chan> <var> body ...) ... (<timeout/seconds> body ...))syntax

Convenience syntax for handling incoming messages from different gochans differently. Used as in Go, typically:

 (chan1 msg (error "from c1" msg))
 (chan2 obj (list  "from c2" obj))
 (1 (error "waited one second, but got nothing!")))

It is an error to specify multiple timeouts. gochan-select returns the associated channel's body return-value.


See ./tests/worker-pool.scm for a port of this Go example.

Contents »