Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

tss_create — create a thread-specific storage key

tss_create creates a key holding a separate pointer value for each thread. Check the result before using the key and plan how resources will be released.

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

Syntax

#include <threads.h>
int tss_create(tss_t *key, tss_dtor_t dtor);

tss_create from <threads.h> writes a key into the tss_t object pointed to by key. One key identifies a separate pointer value for each thread; initially that value is null, including in newly created threads.

Success returns thrd_success. On thrd_error, the key has an indeterminate representation: do not use it or pass it to tss_delete.

dtor may be NULL. tss_delete does not invoke the destructor, and program termination does not invoke TSS destructors either. Do not call tss_create from a destructor.

The example uses tss_set and tss_get in the current thread; it creates no new thread. The pointed-to object remains valid until the key is deleted. A C23 compiler flag does not establish thread-library availability or attest production runtime behavior.

Examples

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

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

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

    if (tss_get(key) != NULL) {
        tss_delete(key);
        return 2;
    }

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

    int *stored = tss_get(key);
    if (stored == NULL) {
        tss_delete(key);
        return 4;
    }

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

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