std1.0.0-beta-7 SafeC docs

Module async::pool

#include <std/async/pool.h> source

async: ThreadPool, the multi-threaded executor

N worker threads (std/thread.h) share one FIFO run queue: a woken task is queued once, and whichever worker is free polls it next (a shared- queue executor, like tokio's multi-thread runtime minus work stealing). The task types are the executor-independent ones from executor.h — AsyncTask, JoinHandle — and so are timers (one TimerQueue shared by the workers), channels and the sync primitives, which all lock internally. A task never runs on two threads at once: the RUNNING/NOTIFIED bits in its header make a wake during a poll re-queue it after the poll.

static struct ThreadPool pool;               // must not move once started
pool = ThreadPool::new();
pool.start(4);
struct JoinHandle<int> h = pool.spawn(&task);   // task storage as for LocalExecutor
int v = pool.block_on(Fetch::new());          // spawn + wait, from a non-worker thread
pool.wait_idle();                              // every spawned task finished
pool.shutdown();                               // stop and join the workers

The futures it runs may be polled on any worker, one after another, so whatever they share must be thread-safe (std/async's primitives are; plain globals need atomics or a lock). I/O futures (std/async/net.h) need a Runtime and don't run here. Hosted targets only.

Structs

ThreadPool

Macros

ASYNC_POOL_MAX_THREADS