How to Add Timed Delay in C++? Last Updated : 23 May, 2024 Comments Improve Suggest changes Like Article Like Report In C++, there is the functionality of delay or inactive state which allows the execution to be delayed for a specific period of time. This is often referred to as a “timed delay”. In this article, we will learn how to add timed delay in C++. Timed Delay in C++To add a timed delay in C++, we can use the std::this_thread::sleep_for() function from the <thread> library. Pass the duration of the delay as a parameter to the sleep_for() function using chrono::seconds(), which will pause the execution of the program for the specified duration. Syntax to Use sleep_for Functionthis_thread::sleep_for(chrono::duration(time_period));Here, duration can be in various time units like seconds, milliseconds, microseconds, etc.time_period is the duration of time for which the current thread is put to sleep.Note: Due to scheduling and other system factors, the actual sleep time may be longer than the specified time. C++ Program to Add Timed DelayThe below program demonstrates how we can add timed delay in C++. C++ // C++ program to Add Timed Delay in C++? #include <iostream> #include <chrono> #include <thread> using namespace std; int main() { // Printing the initial message cout << "Starting the countdown..." << endl; // Countdown loop from 5 to 1 for (int i = 5; i > 0; --i) { // Printing the remaining seconds cout << i << " seconds remaining" << endl; // Waiting for 1 second this_thread::sleep_for(chrono::seconds(1)); } // Printing the final message cout << "Time's up!" << endl; return 0; } Output Starting the countdown...5 seconds remaining4 seconds remaining3 seconds remaining2 seconds remaining1 seconds remainingTime's up!Note: The this_thread::sleep_for() function is part of the C++11 standard, it may not work on older compilers that does not support C++11 and later. Comment More infoAdvertise with us Next Article How to Add Timed Delay in C++? G gpancomputer Follow Improve Article Tags : C++ Programs C++ CPP-Functions CPP Examples Practice Tags : CPP Similar Reads How to Create a Thread in C++? A thread is a basic element of multithreading which represents the smallest sequence of instructions that can be executed independently by the CPU. In this article, we will discuss how to create a thread in C++. How to Create a Thread in C++?In C++, the std::thread is a class template that is used t 2 min read How to Make a Countdown Timer in C++? In C++, countdown timers are valuable components in various applications, aiding in scheduling events, controlling timeouts, or displaying the remaining time. In this article, we will learn how to make a countdown timer in C++. Example: Input: Enter Total Number of Seconds for Countdown Timer: 3Outp 3 min read How to Join a Thread in C++? In C++, a thread is a basic element of multithreading that represents the smallest sequence of instructions that can be executed independently by the CPU. In this article, we will discuss how to join a thread in C++. How to Join a Thread in C++?Joining a thread is a means to wait for the thread to c 2 min read How to Get Time in Milliseconds in C++? In programming measuring accurate time is a common requirement for most software development packages. In C++, we have the std::chrono library with various functions for durations, time, and clocks. In this article, we will learn how to get time in milliseconds in C++. For Example, Input:Current sys 2 min read How to Pause a Program in C++? Pausing programs in C ++ is an important common task with many possible causes like debugging, waiting for user input, and providing short delays between multiple operations. In this article, we will learn how to pause a program in C++. Pause Console in a C++ ProgramWe can use the std::cin::get() me 2 min read Like