Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      thrd_sleep

      From cppreference.com
      <c‎ |thread
       
       
      Concurrency support library
       
      Defined in header<threads.h>
      int thrd_sleep(conststruct timespec* duration,
                     struct timespec* remaining);
      (since C11)

      Blocks the execution of the current thread forat least until theTIME_UTC based duration pointed to byduration has elapsed.

      The sleep may resume earlier if asignal that is not ignored is received. In such case, ifremaining is notNULL, the remaining time duration is stored into the object pointed to byremaining.

      Contents

      [edit]Parameters

      duration - pointer to the duration to sleep for
      remaining - pointer to the object to put the remaining time on interruption. May beNULL, in which case it is ignored

      [edit]Return value

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

      [edit]Notes

      duration andremaining 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 isnanosleep.

      [edit]Example

      Run this code
      #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 secprintf("Time: %s",ctime(&(time_t){time(NULL)}));}

      Output:

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

      [edit]References

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

      [edit]See also

      yields the current time slice
      (function)[edit]
      C++ documentation forsleep_for
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/thread/thrd_sleep&oldid=138888"

      [8]ページ先頭

      ©2009-2025 Movatter.jp