function
<thread>

std::this_thread::sleep_for

template <class Rep, class Period>
  void sleep_for (const chrono::duration<Rep,Period>& rel_time);
Sleep for time span
Blocks execution of the calling thread during the span of time specified by rel_time.

The execution of the current thread is stopped until at least rel_time has passed from now. Other threads continue their execution.

Parameters

rel_time
The time span after which the calling thread shall resume its execution.
Note that multi-threading management operations may cause certain delays beyond this.
duration is an object that represents a specific relative time.

Return value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// this_thread::sleep_for example
#include <iostream>       // std::cout
#include <thread>         // std::this_thread::sleep_for
#include <chrono>         // std::chrono::seconds
 
int main() 
{
  std::cout << "countdown:\n";
  for (int i=10; i>0; --i) {
    std::cout << i << '\n';
    std::this_thread::sleep_for (std::chrono::seconds(1));
  }
  std::cout << "Lift off!\n";

  return 0;
}


Output (after 10 seconds):
countdown:
10
9
8
7
6
5
4
3
2
1
Lift off!

Exception safety

If the type of rel_time never throws exceptions (like the instantiations of duration in header <chrono>), this function never throws exceptions (no-throw guarantee).

See also