Skip to main content

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​

  • work runs on another thread. It must not call Clay, raylib or any ccompose function other than CC_TaskCancelled and CC_TaskSetProgress.
  • arg belongs to the caller. Keep it valid until done has run or the task was cancelled.
  • A cancelled task never gets its done call. Its work keeps running until it returns, so long work should check CC_TaskCancelled between steps.
  • CC_Shutdown cancels every task, drops queued work that has not started, and waits for running work to return.
  • The zero handle {0} is never a live task, so a zero-initialised CC_Task is 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​

NameFormWhat it does
CC_SpawnCC_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_TaskCancelvoid CC_TaskCancel(CC_Task task)Asks the task to stop. Its done is not called.
CC_TaskCancelledbool CC_TaskCancelled(CC_Task task)True after a cancel, during shutdown, or for a stale handle. Call it from work.
CC_TaskSetProgressvoid CC_TaskSetProgress(CC_Task task, float progress)Stores progress from work, clamped to 0 to 1.
CC_TaskProgressfloat CC_TaskProgress(CC_Task task)The last stored progress, or 0. Pass it to LinearProgress.
CC_TaskRunningbool CC_TaskRunning(CC_Task task)True from CC_Spawn until done has run, or until a cancelled task is dropped.
CC_Taskstruct { uint32_t id; }Task handle. {0} means no task.
CC_TaskFnvoid (*)(CC_Task task, void *arg)Signature of work and done.
CC_TASK_MAX64Tasks that can be queued or running at once.