Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

tss_set — setting a thread-specific pointer

tss_set stores a pointer under a thread-specific storage key for the current thread and returns an operation status.

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

Syntax

#include <threads.h>
int tss_set(tss_t key, void *val);

tss_set(key, val) from <threads.h> changes the value only for the current thread. It stores a pointer, not a copy of the pointed-to object; that object must remain available while the pointer is used.

Use a valid key created by successful tss_create, before the thread started executing destructors. The result is thrd_success or thrd_error. NULL is a permitted stored value.

Replacing a value does not invoke its destructor. Manage its resources separately. tss_delete does not invoke destructors either.

The example uses only the main thread; its local variable lives long enough. C23 compilation mode does not attest thread-library availability or production runtime behavior.

Examples

#include <stdio.h>
#include <stdlib.h>
#include <threads.h>

int main(void)
{
    tss_t key;
    int value = 42;

    if (tss_create(&key, NULL) != thrd_success)
        return EXIT_FAILURE;

    if (tss_set(key, &value) != thrd_success) {
        tss_delete(key);
        return EXIT_FAILURE;
    }

    int *stored = tss_get(key);
    if (stored != &value) {
        tss_delete(key);
        return EXIT_FAILURE;
    }

    printf("%d\n", *stored);
    tss_delete(key);
    return EXIT_SUCCESS;
}

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