Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

thrd_current — current thread identifier

The thrd_current function from <threads.h> returns the calling thread's identifier as a value of type thrd_t.

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

Syntax

#include <threads.h>
thrd_t thrd_current(void);

thrd_current() takes no arguments and returns the calling thread's identifier. It does not create a new thread. Its declaration is in <threads.h>; N3096 describes it in §7.28.5.2.

Compare identifiers with thrd_equal, without assuming a numeric representation for thrd_t. A nonzero result means the same thread; zero means different threads.

The example retrieves the identifier twice in main. The expression same != 0 normalizes the result to 1. A thread-library implementation is required; the repository's C23 flag alone does not attest support in the production runtime.

Examples

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

int main(void)
{
    thrd_t self = thrd_current();
    int same = thrd_equal(self, thrd_current());
    printf("same thread: %d\n", same != 0);
    return 0;
}

Result

same thread: 1

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