Tasks
CC_Spawn runs slow work, such as loading a file, waiting on the network or a long computation, on a worker thread so frames keep drawing. When the work returns, the next CC_Begin calls its done callback on the UI thread. That callback is the place to update app state or call Clay and raylib, for example LoadTextureFromImage. Declared in ccompose/task.h. Works headless.
Example
typedef struct { const char *path; Image img; Texture2D tex; } Photo;
static void load(CC_Task t, void *arg) { // worker thread
Photo *p = arg;
p->img = LoadImage(p->path); // CPU only, no GPU calls here
CC_TaskSetProgress(t, 1.0f);
}
static void loaded(CC_Task t, void *arg) { // UI thread, inside CC_Begin
Photo *p = arg;
p->tex = LoadTextureFromImage(p->img);
UnloadImage(p->img);
}
static Photo photo = { .path = "photo.png" };
static CC_Task task; // zero handle: no task yet
static void frame(void) {
CC_Begin();
Column("Root") {
if (Button("Open") && !CC_TaskRunning(task))
task = CC_Spawn(load, loaded, &photo);
if (CC_TaskRunning(task)) {
CircularProgress(CC_PROGRESS_INFINITE);
if (Button("Cancel")) CC_TaskCancel(task);
}
}
CC_End();
}
Rules
workruns on another thread. It must not call Clay, raylib or any ccompose function other thanCC_TaskCancelledandCC_TaskSetProgress.argbelongs to the caller. Keep it valid untildonehas run or the task was cancelled.- A cancelled task never gets its
donecall. Itsworkkeeps running until it returns, so long work should checkCC_TaskCancelledbetween steps. CC_Shutdowncancels every task, drops queued work that has not started, and waits for runningworkto return.- The zero handle
{0}is never a live task, so a zero-initialisedCC_Taskis safe to pass anywhere. A handle goes stale once its task ends, and every function treats a stale handle as a task that does not exist. - Four worker threads take queued tasks oldest first. Web builds without pthreads run one queued task per frame inside
CC_Begin, which blocks that frame.
Reference
| Name | Form | What it does |
|---|---|---|
CC_Spawn | CC_Task CC_Spawn(CC_TaskFn work, CC_TaskFn done, void *arg) | Queues work on a worker thread. done may be NULL. Returns the zero handle when work is NULL or CC_TASK_MAX tasks are live. |
CC_TaskCancel | void CC_TaskCancel(CC_Task task) | Asks the task to stop. Its done is not called. |
CC_TaskCancelled | bool CC_TaskCancelled(CC_Task task) | True after a cancel, during shutdown, or for a stale handle. Call it from work. |
CC_TaskSetProgress | void CC_TaskSetProgress(CC_Task task, float progress) | Stores progress from work, clamped to 0 to 1. |
CC_TaskProgress | float CC_TaskProgress(CC_Task task) | The last stored progress, or 0. Pass it to LinearProgress. |
CC_TaskRunning | bool CC_TaskRunning(CC_Task task) | True from CC_Spawn until done has run, or until a cancelled task is dropped. |
CC_Task | struct { uint32_t id; } | Task handle. {0} means no task. |
CC_TaskFn | void (*)(CC_Task task, void *arg) | Signature of work and done. |
CC_TASK_MAX | 64 | Tasks that can be queued or running at once. |