CKTIS / C
cnd_signal — C23 condition-variable function
`cnd_signal(cond)` unblocks one thread blocked on `cond` at the time of the call. If there is no waiter, it still does nothing and returns `thrd_success`; otherwise the result is `thrd_success` or `thrd_error`.
Version: C23
Syntax
int cnd_signal(cnd_t *cond);cnd_signal(cond) unblocks one thread blocked on cond at the time of the call. If there is no waiter, it still does nothing and returns thrd_success; otherwise the result is thrd_success or thrd_error.
The example uses waiting plus the shared mutex to ensure one worker is genuinely waiting. The main thread stores protected payload = 42 and sets ready, calls cnd_signal, and after joining prints 1 42.
There is no guarantee which of several waiters is selected, and an early signal is not queued. A signal does not replace data protection: change the predicate under the same mutex and wait in a loop because a thread may unblock for an unspecified reason.
Examples
#include <stdatomic.h>
#include <stdio.h>
#include <threads.h>
static mtx_t gate;
static cnd_t condition;
static atomic_int waiting;
static int ready;
static int payload;
static int observed;
static int worker(void *unused) {
(void)unused;
if (mtx_lock(&gate) != thrd_success) return 1;
atomic_store(&waiting, 1);
while (!ready) {
if (cnd_wait(&condition, &gate) != thrd_success) {
mtx_unlock(&gate);
return 1;
}
}
observed = payload;
return mtx_unlock(&gate) == thrd_success ? 0 : 1;
}
int main(void) {
if (mtx_init(&gate, mtx_plain) != thrd_success) return 1;
if (cnd_init(&condition) != thrd_success) return 1;
thrd_t thread;
if (thrd_create(&thread, worker, NULL) != thrd_success) return 1;
while (!atomic_load(&waiting)) thrd_yield();
if (mtx_lock(&gate) != thrd_success) return 1;
payload = 42;
ready = 1;
int result = cnd_signal(&condition);
if (mtx_unlock(&gate) != thrd_success) return 1;
int worker_result;
thrd_join(thread, &worker_result);
printf("%d %d\n", result == thrd_success && worker_result == 0, observed);
cnd_destroy(&condition);
mtx_destroy(&gate);
return 0;
}
Result
1 42
The example uses `waiting` plus the shared mutex to ensure one worker is genuinely waiting. The main thread stores protected `payload = 42` and sets `ready`, calls `cnd_signal`, and after joining prints `1 42`.