Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

<threads.h> — C threads

The <threads.h> header provides threads, synchronization, and thread-specific data. Check availability in your implementation.

Version: C23; repository compiler flag, production runtime not attested

Syntax

#include <threads.h>

<threads.h> declares facilities for threads (thrd_*), mutexes (mtx_*), condition variables (cnd_*), thread-specific data (tss_*), and call_once. The thread interfaces use struct timespec, but including <threads.h> does not guarantee that the other declarations and macros from <time.h> are available. Include <time.h> directly if you need them.

An implementation defining __STDC_NO_THREADS__ need not provide this header. C23 compiler mode alone does not establish library availability or production runtime support.

The example checks operation results. thrd_join waits for thread termination and stores its int result; this is distinct from the success status of thrd_join itself. On success, the program prints 42. Concurrent access to shared data requires appropriate synchronization; volatile is not a substitute.

Examples

#include <stdio.h>

#ifdef __STDC_NO_THREADS__
#error "C threads support required"
#else
#include <threads.h>

static int worker(void *arg)
{
    (void)arg;
    return 42;
}

int main(void)
{
    thrd_t thread;
    int result;

    if (thrd_create(&thread, worker, NULL) != thrd_success)
        return 1;
    if (thrd_join(thread, &result) != thrd_success)
        return 1;

    printf("%d\n", result);
    return 0;
}
#endif

Result

42

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources