Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

tss_delete — deleting a thread-specific storage key

tss_delete releases resources for a thread-specific storage key, but does not invoke destructors or automatically free the pointed-to data.

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

Syntax

#include <threads.h>
void tss_delete(tss_t key);

tss_delete(key) from <threads.h> releases resources for the thread-specific storage identified by key. It returns void. Use a key successfully created by tss_create, before the calling thread begins executing destructors.

Deleting the key invokes no destructors: memory allocated with malloc needs separate cleanup. Coordinate all threads to finish using the key before deleting it. If another thread is executing destructors, the effect of deletion on that thread's invocation count for this key's destructor is unspecified.

The example retains the pointer, deletes the key, and frees the object itself. The counter demonstrates that the destructor was not called.

Examples

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

static int destructor_calls;

static void destroy(void *ptr)
{
    ++destructor_calls;
    free(ptr);
}

int main(void)
{
    tss_t key;
    if (tss_create(&key, destroy) != thrd_success) {
        return EXIT_FAILURE;
    }

    int *value = malloc(sizeof *value);
    if (value == NULL) {
        tss_delete(key);
        return EXIT_FAILURE;
    }
    *value = 42;

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

    tss_delete(key);
    printf("destructor calls: %d\n", destructor_calls);
    free(value);
    return EXIT_SUCCESS;
}

Result

destructor calls: 0

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