thrd_sleep

C
 Language Headers Type support Dynamic memory management Error handling Program utilities Variadic function support Date and time utilities Strings library Algorithms Numerics Input/output support Localization support Thread support (C11) Atomic operations (C11) Technical Specifications

 thrd_create thrd_equal thrd_current thrd_sleep thrd_yield thrd_exit
Mutual exclusion
Condition variables

 Defined in header  int thrd_sleep( const struct timespec* time_point,                 struct timespec* remaining ); (since C11)

Blocks the execution of the current thread for at least until the TIME_UTC based time point pointed to by time_point has been reached.

The sleep may resume earlier if a signal that is not ignored is received. In such case, if remaining is not NULL, the remaining time duration is stored into the object pointed to by remaining.

Contents

Parameters

 time_point - pointer to the time point to sleep until remaining - pointer to the object to put the remaining time on interruption. May be NULL, in which case it is ignored

Return value

0 on successful sleep, -1 if a signal occurred, other negative value if an error occurred.

Notes

time_point and remaining may point at the same object, which simplifies re-running the function after a signal.

The actual sleep time may be longer than requested because it is rounded up to the timer granularity and because of scheduling and context switching overhead.

The POSIX equivalent of this function is nanosleep

Example

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

int main(void)
{
printf("Time: %s", ctime(&(time_t){time(NULL)}));
thrd_sleep(&(struct timespec){.tv_sec=1}, NULL); // sleep 1 sec
printf("Time: %s", ctime(&(time_t){time(NULL)}));
}

Output:

Time: Mon Feb  2 16:18:41 2015
Time: Mon Feb  2 16:18:42 2015

References

• C11 standard (ISO/IEC 9899:2011):
• 7.26.5.7 The thrd_sleep function (p: 385)