Detail C++11 thread sleep function

  • 2020-11-25 07:26:10
  • OfStack

C++ 11 did not provide a specific sleep function before. In fact, sleep and usleep of c language are functions provided by the system, and the functions of different system functions are somewhat different.

In the Windows system, the parameter of sleep is milliseconds.


sleep(2*1000); //sleep for 2 seconds

In Unix class systems, the sleep() function is measured in seconds.


sleep(2);  //sleep for 2 seconds

Starting with C++11, the C++ standard library provides a dedicated thread sleep function that enables your code to be independent of different platforms.


std::this_thread::sleep_for

std::this_thread::sleep_untill

1. Let the thread sleep for a period of time

std::this_thread::sleep_for for Block current thread 1 period of time.


template< class Rep, class Period >
void sleep_for( const std::chrono::duration<Rep, Period>& sleep_duration );

The sleep interval is defined in terms ranging from nanoseconds to hours.


std::chrono::nanoseconds
std::chrono::microseconds
std::chrono::milliseconds
std::chrono::seconds
std::chrono::minutes
std::chrono::hours

Let's say we want one thread to sleep for 100ms.


std::this_thread::sleep_for(std::chrono::milliseconds(100));

We want 1 thread to sleep for 1 minute:


std::this_thread::sleep_for(std::chrono::minutes(1));

Complete code example:


#include <iostream>
#include <chrono>
#include <thread>
 
int main() {
  std::cout << "Hello waiter\n" << std::flush;

  auto start = std::chrono::high_resolution_clock::now();

  std::this_thread::sleep_for(std::chrono::milliseconds(2000));
  
  auto end = std::chrono::high_resolution_clock::now();
  
  std::chrono::duration<double, std::milli> elapsed = end-start;
  std::cout << "Waited " << elapsed.count() << " ms\n";
}

Output:

[

Hello waiter
Waited 2000.12 ms

]

2. Hibernate until you reach a time point


template< class Clock, class Duration >
void sleep_until( const std::chrono::time_point<Clock,Duration>& sleep_time )

sleep_until blocks the current thread until it reaches some point in the future.


#include <iostream>
#include <thread>
#include <chrono>

// Print Current Time
void print_time_point(std::chrono::system_clock::time_point timePoint) {
  std::time_t timeStamp = std::chrono::system_clock::to_time_t(timePoint);
  std::cout << std::ctime(&timeStamp) << std::endl;
}

void threadFunc() {
  std::cout<<"Current Time :: ";
  // Print Current Time
  print_time_point(std::chrono::system_clock::now());
  // create a time point pointing to 10 second in future
  std::chrono::system_clock::time_point timePoint =
      std::chrono::system_clock::now() + std::chrono::seconds(10);
  std::cout << "Going to Sleep Until :: "; print_time_point(timePoint);
  // Sleep Till specified time point
  // Accepts std::chrono::system_clock::time_point as argument
  std::this_thread::sleep_until(timePoint);
  std::cout<<"Current Time :: ";
  // Print Current Time
  print_time_point(std::chrono::system_clock::now());
}

int main() {
  std::thread th(&threadFunc);
  th.join();

  return 0;
}

Program output:

[

Current Time :: Sun Oct 11 02:57:38 2020

Going to Sleep Until :: Sun Oct 11 02:57:48 2020

Current Time :: Sun Oct 11 02:57:48 2020

]

Reference material

1.https://baike.baidu.com/item/sleep%E5%87%BD%E6%95%B0/6735027

2.https://thispointer.com/how-to-put-a-thread-to-sleep-in-c11-sleep_for-sleep_until/

That's the details of C++11 thread sleep function. For more information on C++11 thread sleep function, please follow other related articles on this site!


Related articles: