Sleep in C++
This post will discuss how to add a time delay to a C++ program. In other words, implement sleep in C++.
1. Using sleep_for() function
Since C++11, we can use std::this_thread::sleep_for function to block the execution of the current thread for the specified duration. The duration can be of type std::chrono::nanoseconds, std::chrono::microseconds, std::chrono::milliseconds, std::chrono::seconds, std::chrono::minutes, or std::chrono::hours.
The std::this_thread::sleep_for function and above durations are defined in the namespace std::chrono. Note that C++20 introduced a few more durations – std::chrono::days, std::chrono::weeks, std::chrono::months, and std::chrono::years to the standard.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> #include <chrono> #include <thread> int main() { std::cout << "Sleeping for 1 second.." << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // sleep for 1 second std::cout << "1 second elapsed.." << std::endl; return 0; } |
With C++14, we can further simplify the code using the literal suffixes (ns, us, ms, s, h, etc.) for the std::chrono durations. This would need the std::chrono_literals namespace.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <chrono> #include <thread> int main() { std::cout << "Sleeping for 1 second.." << std::endl; using namespace std::chrono_literals; std::this_thread::sleep_for(1s); // sleep for 1 second std::cout << "1 second elapsed.." << std::endl; return 0; } |
2. Using sleep_until() function
C++ also provides the std::this_thread::sleep_until function, which blocks the execution of the current thread until the specified duration has been reached. It can be used as follows to add a time delay:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <chrono> #include <thread> int main() { std::cout << "Sleeping for 1 second.." << std::endl; // add time delay of 1 second std::this_thread::sleep_until(std::chrono::system_clock::now() + std::chrono::seconds(1)); std::cout << "1 second elapsed.." << std::endl; return 0; } |
Starting with C++14, consider using the literal suffixes (ns, us, ms, s, h, etc.) from the std::chrono_literals namespace.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <chrono> #include <thread> int main() { std::cout << "Sleeping for 1 second.." << std::endl; using namespace std::chrono_literals; std::this_thread::sleep_until(std::chrono::system_clock::now() + 1s); std::cout << "1 second elapsed.." << std::endl; return 0; } |
3. Using Sleep() function
In a Windows environment, we can use the sleep() function to suspend the execution of the current thread until the specified number of milliseconds elapses. This is available in the <windows.h> header.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> #include <windows.h> int main() { std::cout << "Sleeping for 1 second.." << std::endl; Sleep(1000); // sleep for 1 second std::cout << "1 second elapsed.." << std::endl; return 0; } |
Alternatively, we can use the sleep() function in Unix environment which sleeps for the specified number of seconds. This requires <unistd.h> header.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> #include <unistd.h> int main() { std::cout << "Sleeping for 1 second.." << std::endl; sleep(1); // sleep for 1 second std::cout << "1 second elapsed.." << std::endl; return 0; } |
To suspend execution of a current thread for microsecond intervals, use the usleep() function instead. The nanosleep() is also available, which takes nanosecond interval.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> #include <unistd.h> int main() { std::cout << "Sleeping for 1 second.." << std::endl; usleep(1000000); // sleep for 1 second std::cout << "1 second elapsed.." << std::endl; return 0; } |
4. Using Boost
Finally, we can use the date-time functions offered by the boost library. The following solution uses boost::this_thread::sleep from header <boost/thread/thread.hpp> to sleep for the specified duration:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <boost/thread/thread.hpp> int main() { std::cout << "Sleeping for 1 second.." << std::endl; boost::this_thread::sleep(boost::posix_time::seconds(1)); // or // boost::this_thread::sleep(boost::posix_time::milliseconds(1000)); std::cout << "1 second elapsed.." << std::endl; return 0; } |
That’s all about adding a time delay to a C++ program.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)