Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

thrd_yield — allowing other threads to run

The thrd_yield function from <threads.h> attempts to allow other threads to run. It does not guarantee a context switch, takes no arguments, and returns no value.

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

Syntax

#include <threads.h>
void thrd_yield(void);

thrd_yield() attempts to allow other threads to run even when the current thread would normally continue executing. <threads.h> provides the declaration void thrd_yield(void);; the function takes no arguments and returns no value.

It guarantees neither that another thread runs nor any execution order. It is neither a timed delay nor a synchronization mechanism: it does not replace mutexes or atomic operations when accessing shared data.

The example calls the function in the main thread. The expected lines are before followed by after; they do not demonstrate a thread switch. The macro __STDC_NO_THREADS__ with value 1 indicates no support for <threads.h>. A compiler flag selecting C23 alone does not attest library availability in a production runtime.

Examples

#include <stdio.h>

#if defined(__STDC_NO_THREADS__) && __STDC_NO_THREADS__
#error "C threads support is required"
#endif

#include <threads.h>

int main(void)
{
    puts("before");
    thrd_yield();
    puts("after");
    return 0;
}

Result

before
after

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