CKTIS / C
thrd_sleep — suspend the calling thread
thrd_sleep suspends the calling thread for a requested interval. A signal can interrupt the wait, and scheduling can extend it.
Version: C23; repository compiler flag, production runtime not attested
Syntax
#include <threads.h>
int thrd_sleep(const struct timespec *duration, struct timespec *remaining);thrd_sleep from <threads.h> takes an interval, not a deadline: duration points to a struct timespec containing seconds in tv_sec and nanoseconds in tv_nsec.
remaining may be NULL; on signal interruption it receives the remaining interval. Both pointers may designate the same object.
A return value of 0 means the requested interval elapsed. If a signal interrupts the wait, the function returns -1. On failure it returns a negative value, which may also be -1. Thus -1 alone does not tell you whether a signal interrupted the wait; do not assume it is safe to retry on that basis.
Without interruption, sleep is not shorter than requested according to TIME_UTC, but clock resolution and scheduling can extend it.
Examples
#include <stdio.h>
#include <stdlib.h>
#include <threads.h>
#include <time.h>
int main(void)
{
const struct timespec duration = {
.tv_sec = 0,
.tv_nsec = 10000000L
};
if (thrd_sleep(&duration, NULL) != 0) {
fputs("Sleep did not complete.\n", stderr);
return EXIT_FAILURE;
}
puts("Sleep completed.");
return EXIT_SUCCESS;
}
Result
Sleep completed.
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.