libdill: Structured Concurrency for C


go - start a coroutine


#include <libdill.h>

int go(expression);


Launches a coroutine that executes the function invocation passed as argument. The coroutine is executed concurrently, and its lifetime may exceed the lifetime of the caller. The return value of the coroutine, if any, is discarded and cannot be retrieved by the caller.

Any function to be invoked with go() must be declared with the coroutine specifier.

WARNING: Coroutines will most likely work even without the coroutine specifier. However, they may fail in random non-deterministic ways, depending on the code in question and the particular combination of a compiler and optimization level. Additionally, arguments to a coroutine must not be function calls. If they are, the program may fail non-deterministically. If you need to pass a result of a computation to a coroutine, do the computation first, and then pass the result as an argument. Instead of:


Do this:

int a = foo();


Returns a coroutine handle. In the case of an error, it returns -1 and sets errno to one of the values below.



coroutine void add(int a, int b) {
    printf("%d+%d=%d\n", a, b, a + b);


int h = go(add(1, 2));