Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

thrd_join — wait for thread termination

thrd_join blocks the calling thread until the specified thread terminates and optionally retrieves its int result. The operation status is separate from the thread result.

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

Syntax

#include <threads.h>
int thrd_join(thrd_t thr, int *res);

thrd_join from <threads.h> waits for thread thr to terminate. A non-null res receives its result; NULL discards the result.

It returns thrd_success or thrd_error, not the thread result. That result comes from the thread function's return or thrd_exit. Read it only after success.

Thread termination synchronizes with completion of thrd_join. The target must not already have been detached or joined. Do not assume violating this requirement returns thrd_error.

Examples

#include <stdio.h>
#include <stdlib.h>
#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 EXIT_FAILURE;

    if (thrd_join(thread, &result) != thrd_success)
        return EXIT_FAILURE;

    printf("result = %d\n", result);
    return EXIT_SUCCESS;
}

Result

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