How to put a thread to sleep in c++11 ? | sleep_for | sleep_until

In this article we will discuss how to put a c++11 thread to sleep.

c++11 provides 2 functions for putting a thread to sleep i.e.

std::this_thread::sleep_for
std::this_thread::sleep_untill

Sleep for a Duration

C++11 provides a function std::this_thread::sleep_for to block the current thread for specified duration i.e.

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

This function accepts a duration as an argument and make the calling thread to sleep for that particular duration.

This duration can be from nanoseconds to hours i.e.

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

Lets see some examples,

Advertisements

Sleeping a Thread for MilliSeconds:

To sleep a thread for 200 Milliseconds call sleep_for with following argument i.e.

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

Sleeping a Thread for Minutes:

To sleep a thread for 1 Minute call sleep_for with following argument i.e.

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

Checkout complete example as follows,
#include <iostream>
#include <thread>
#include <chrono>

void threadFunc()
{
	int i = 0;
	while (i < 10)
	{
		// Print Thread ID and Counter i
		std::cout<<std::this_thread::get_id()<<" :: "<<i++<<std::endl;

		// Sleep this thread for 200 MilliSeconds
		std::this_thread::sleep_for(std::chrono::milliseconds(200));
	}
}

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

Output:
140484807997184 :: 0
140484807997184 :: 1
140484807997184 :: 2
140484807997184 :: 3
140484807997184 :: 4
140484807997184 :: 5
140484807997184 :: 6
140484807997184 :: 7
140484807997184 :: 8
140484807997184 :: 9

Sleep Until a TimePoint

Many times we want the thread to sleep untill a time point in future. That can be acieved using sleep_untill() i.e.

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

It accepts a time point as an argument and blocks the current thread till this time point is achieved.

Checkout the complete example, here we will put a thread to sleep until a time point in future i.e.

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

Output:
Current Time :: Sat Feb 25 16:44:40 2017
Going to Sleep Until :: Sat Feb 25 16:44:50 2017
Current Time :: Sat Feb 25 16:44:50 2017

 

Do you want to Learn Modern C++ from best?

We have curated a list of Best C++ Courses, that will teach you the cutting edge Modern C++ from the absolute beginning to advanced level. It will also introduce to you the word of Smart Pointers, Move semantics, Rvalue, Lambda function, auto, Variadic template, range based for loops, Multi-threading and many other latest features of C++ i.e. from C++11 to C++20.

Check Detailed Reviews of Best Modern C++ Courses

Remember, C++ requires a lot of patience, persistence, and practice. So, start learning today.

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top