libdill: Structured Concurrency for C


choose - performs one of multiple channel operations


#include <libdill.h>

struct chclause {
    int op;
    int ch;
    void *val;
    size_t len;

int choose(
    struct chclause* clauses,
    int nclauses,
    int64_t deadline);


Accepts a list of channel operations. Performs one that can be done first. If multiple operations can be done immediately, the one that comes earlier in the array is executed.

clauses: Operations to choose from. See below.

nclauses: Number of clauses.

deadline: A point in time when the operation should time out, in milliseconds. Use the now function to get your current point in time. 0 means immediate timeout, i.e., perform the operation if possible or return without blocking if not. -1 means no deadline, i.e., the call will block forever if the operation cannot be performed.

The fields in chclause structure are as follows:


In case of success the function returns index of the clause that caused the function to exit. In case of error it returns -1 and sets errno to one of the values below.

Even if an index is returned, errno may still be set to an error value. The operation was successfull only if errno is set to zero.


Additionally, if the function returns an index it can set errno to one of the following values:


int val1 = 0;
int val2;
struct chclause clauses[] = {
    {CHSEND, ch, &val1, sizeof(val1)},
    {CHRECV, ch, &val2, sizeof(val2)}
int rc = choose(clauses, 2, now() + 1000);
if(rc == -1) {
    perror("Choose failed");
if(rc == 0) {
    printf("Value %d sent.\n", val1);
if(rc == 1) {
    printf("Value %d received.\n", val2);


chdone(3) chmake(3) chmake_mem(3) chrecv(3) chsend(3) now(3)