函式
<thread>

std::this_thread::sleep_until

template <class Clock, class Duration>  void sleep_until (const chrono::time_point<Clock,Duration>& abs_time);
休眠至指定時間點
將呼叫執行緒阻塞直到 abs_time

當前執行緒的執行將暫停,直到至少到達 abs_time,而其他執行緒則可能繼續執行。

引數

abs_time
呼叫執行緒應恢復其執行的時間點。
請注意,多執行緒管理操作可能會導致超過此時間的延遲。
time_point 是表示特定絕對時間的物件。

返回值



示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// this_thread::sleep_for example
#include <iostream>       // std::cout
#include <iomanip>        // std::put_time
#include <thread>         // std::this_thread::sleep_until
#include <chrono>         // std::chrono::system_clock
#include <ctime>          // std::time_t, std::tm, std::localtime, std::mktime

int main() 
{
  using std::chrono::system_clock;
  std::time_t tt = system_clock::to_time_t (system_clock::now());

  struct std::tm * ptm = std::localtime(&tt);
  std::cout << "Current time: " << std::put_time(ptm,"%X") << '\n';

  std::cout << "Waiting for the next minute to begin...\n";
  ++ptm->tm_min; ptm->tm_sec=0;
  std::this_thread::sleep_until (system_clock::from_time_t (mktime(ptm)));

  std::cout << std::put_time(ptm,"%X") << " reached!\n";

  return 0;
}

(平均 30 秒後) 輸出
Current time: 11:52:36
Waiting for the next minute to begin...
11:53:00 reached!


異常安全

如果 abs_time 的型別永遠不會丟擲異常(例如,如果它僅使用標頭 <chrono> 中提供的標準時鐘的值),則此函式永遠不會丟擲異常(無丟擲保證)。

另見