Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::this_thread::sleep_until

      From cppreference.com
      <cpp‎ |thread
       
       
      Concurrency support library
      Threads
      (C++11)
      (C++20)
      this_thread namespace
      (C++11)
      (C++11)
      (C++11)
      sleep_until
      (C++11)
      Cooperative cancellation
      Mutual exclusion
      Generic lock management
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      Condition variables
      (C++11)
      Semaphores
      Latches and Barriers
      (C++20)
      (C++20)
      Futures
      (C++11)
      (C++11)
      (C++11)
      Safe reclamation
      Hazard pointers
      Atomic types
      (C++11)
      (C++20)
      Initialization of atomic types
      (C++11)(deprecated in C++20)
      (C++11)(deprecated in C++20)
      Memory ordering
      (C++11)(deprecated in C++26)
      Free functions for atomic operations
      Free functions for atomic flags
       
      Defined in header<thread>
      template<class Clock,class Duration>
      void sleep_until(conststd::chrono::time_point<Clock, Duration>& sleep_time);
      (since C++11)

      Blocks the execution of the current thread until specifiedsleep_time has been reached.

      Clock must meet theClock requirements. The program is ill-formed ifstd::chrono::is_clock_v<Clock> isfalse.(since C++20)

      The standard recommends that the clock tied tosleep_time be used, in which case adjustments of the clock may be taken into account. Thus, the duration of the block might be more or less thansleep_time- Clock::now() at the time of the call, depending on the direction of the adjustment and whether it is honored by the implementation. The function also may block until aftersleep_time has been reached due to process scheduling or resource contention delays.

      Contents

      [edit]Parameters

      sleep_time - time to block until

      [edit]Return value

      (none)

      [edit]Exceptions

      Any exception thrown byClock orDuration (clocks and durations provided by the standard library never throw).

      [edit]Example

      Run this code
      #include <chrono>#include <iostream>#include <thread> auto now(){returnstd::chrono::steady_clock::now();} auto awake_time(){using std::chrono::operator""ms;return now()+ 2000ms;} int main(){std::cout<<"Hello, waiter...\n"<<std::flush;constauto start{now()};    std::this_thread::sleep_until(awake_time());std::chrono::duration<double,std::milli> elapsed{now()- start};std::cout<<"Waited "<< elapsed.count()<<" ms\n";}

      Possible output:

      Hello, waiter...Waited 2000.17 ms

      [edit]See also

      (C++11)
      stops the execution of the current thread for a specified time duration
      (function)[edit]
      C documentation forthrd_sleep
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/thread/sleep_until&oldid=161243"

      [8]ページ先頭

      ©2009-2025 Movatter.jp