<thread>

this_thread

function
<thread>

std::this_thread::sleep_until

template <class Clock, class Duration>  void sleep_until (const chrono::time_point<Clock,Duration>& abs_time);
Sleep until time point
Blocks the calling thread until abs_time.

The execution of the current thread is stopped until at least abs_time, while other threads may continue to advance.

Parameters

abs_time
A point in time when the calling thread shall resume its execution.
Note that multi-threading management operations may cause certain delays beyond this.
time_point is an object that represents a specific absolute time.

Return value

none

Example

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;
}

Output (after an avg. of 30 seconds):
Current time: 11:52:36
Waiting for the next minute to begin...
11:53:00 reached!


Exception safety

If the type of abs_time never throws exceptions (like if it only uses values from the standard clocks provided in header <chrono>), this function never throws exceptions (no-throw guarantee).

See also