HOWTO · C++
How to Add Timed Delay in C++
This tutorial will give you a brief guide on adding a timed delay in your C++ program.
On this page
A C++ program can pause its current thread with a platform API or with the standard library’s thread waits. Choose based on portability and whether your deadline is a duration or a clock time: sleep() uses whole seconds on POSIX systems, usleep() uses microseconds on POSIX systems, and C++11’s sleep_for() and sleep_until() accept durations and time points. These calls suspend the calling thread; they do not guarantee that it resumes at an exact instant.
Choose a C++ delay API
Use sleep_for() for a portable C++11 relative wait in ordinary application code. Use sleep_until() when work should wait until a deadline on a clock. The POSIX functions sleep() and usleep() are useful when code is already tied to POSIX; they are not portable C++ facilities. Windows provides its own Sleep() API, whose argument is in milliseconds.
A delay blocks the thread that calls it. If a user interface, server, or worker pool needs to remain responsive, pause a dedicated thread or use the framework’s asynchronous timer instead of blocking its main or event-loop thread.
Use sleep() for whole-second, platform-specific delays
C++ does not define a portable sleep() function. On POSIX systems, declare it with <unistd.h> and call sleep(seconds). It returns the number of whole seconds still requested if a signal interrupts the wait, or zero when the requested interval elapses. On Windows, include <Windows.h> and call Sleep(milliseconds); its unit is milliseconds, so a one-second delay is Sleep(1000). The POSIX and Windows calls have different names, headers, and units. The Windows header is also available in the Windows.h guide.
For example, the POSIX call is sleep(2); after including <unistd.h>. On Windows, use Sleep(2000); after including <Windows.h>. Select the branch for the target platform; a POSIX declaration will not make sleep() available on Windows.
Use usleep() for POSIX microsecond delays
usleep() is declared by <unistd.h> on POSIX systems and accepts a duration in microseconds. For example, usleep(250000); requests a quarter-second pause. It is a POSIX interface rather than portable C++; some platforms or feature settings may omit it, so use sleep_for() for cross-platform C++ code. The function returns 0 when the wait completes and -1 when interrupted or otherwise unsuccessful; check the return value if the caller must react to interruption. Actual elapsed time can exceed the requested interval because the operating system schedules threads in its own time.
Use sleep_for() for a relative delay
Include <thread> and <chrono> to wait for a relative duration. sleep_for() suspends the current thread for at least the requested duration, subject to scheduling and resource contention. This complete C++11 program pauses for 200 milliseconds and then prints a confirmation. Run this example:
#include <chrono>
#include <iostream>
#include <thread>
int main() {
std::this_thread::sleep_for(std::chrono::milliseconds(200));
std::cout << "Wait complete\n";
}
Wait complete
Use a duration type that makes the intended unit clear, such as std::chrono::seconds(2) or std::chrono::milliseconds(250). The wait affects only the calling thread, and scheduler delays can make the elapsed time longer than the duration passed to the function.
Use sleep_until() to wait for a time point
sleep_until() also comes from <thread>, but it accepts a clock time point rather than a duration. Add a duration to a clock’s now() value to form a future deadline; for example, std::this_thread::sleep_until(std::chrono::steady_clock::now() + std::chrono::seconds(3));. This waits until that point on steady_clock, which is intended for measuring intervals and is not adjusted like wall-clock time. A deadline already in the past does not add a wait. The function returns no value, and scheduling can resume the thread after the deadline.
Prefer steady_clock for elapsed-time deadlines so a system clock correction does not move the target. Use system_clock only when the target is a wall-clock time, and account for changes to that clock while waiting. Like sleep_for(), this blocks the calling thread.
Choose the right delay and account for scheduling
For portable C++11 code, use sleep_for() when the requirement is “wait this long” and sleep_until() when it is “wait until this deadline.” Use sleep() or usleep() only when POSIX-specific behavior is appropriate, and use Windows Sleep() in Windows-specific code. Keep the units visible: sleep() takes seconds, usleep() takes microseconds, Windows Sleep() takes milliseconds, and <chrono> represents durations with named types.
None of these APIs is a precision timer. The requested time is a minimum waiting target, not a promise about the exact resume time; scheduling, contention, and interruption behavior affect when execution continues. For short waits that must not block a thread, use an asynchronous timer supplied by the platform or framework. This distinction helps choose a delay that fits both the program’s portability needs and its timing model.