Time is Money: How to Put a C++ Thread to Sleep for Milliseconds

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

When Every Millisecond Counts: Why You Need Precise Sleep in C++

\n\n

Imagine you are writing a game where enemies attack every 500 ms. Or you are developing an industrial controller that polls a sensor strictly every 10 ms. Or you simply want to create a loading animation with smooth text appearance. In all these cases, you will need the command: sleep for milliseconds.

\n\n

It seems simple enough — to "put a thread to sleep" for a given time. But in C++, unlike Python or JavaScript, there is no single built-in function to "wait N milliseconds." More precisely, it exists, but it differs across operating systems and standards. There are also pitfalls: precision, portability, and performance impact.

\n\n

In this article, we will cover all the ways to create a delay in C++ for milliseconds: from the classic Sleep() and usleep() to the modern std::this_thread::sleep_for(). You will get ready-made code examples, learn which method to choose for your task, and how to avoid common mistakes.

\n\n

The Standard Way: std::this_thread::sleep_for (C++11 and newer)

\n\n

Starting with the C++11 standard, the language introduced the <chrono> library and the ability for a portable thread delay. This is the most correct and recommended method for modern C++ code.

\n\n

How It Works

\n\n

The function std::this_thread::sleep_for() takes a time interval specified using std::chrono::duration. You simply specify the number of milliseconds, and the thread "sleeps" for that time.

\n\n

Example 1: Basic 500 ms Delay

\n\n
#include <iostream>\n#include <thread>\n#include <chrono>\n\nint main() {\n    std::cout << "Starting countdown...\\n";\n    \n    // Delay for 500 milliseconds\n    std::this_thread::sleep_for(std::chrono::milliseconds(500));\n    \n    std::cout << "Half a second has passed!\\n";\n    return 0;\n}
\n\n

This code will work on Windows, Linux, macOS, and any other platform with a C++11/14/17/20 compiler. No #ifdef, no system calls.

\n\n

Example 2: Delay with a Variable

\n\n

If you need to set the time dynamically:

\n\n
#include <iostream>\n#include <thread>\n#include <chrono>\n\nvoid process_with_delay(int delay_ms) {\n    std::cout << "Processing with delay " << delay_ms << " ms...\\n";\n    std::this_thread::sleep_for(std::chrono::milliseconds(delay_ms));\n    std::cout << "Done!\\n";\n}\n\nint main() {\n    process_with_delay(200);\n    process_with_delay(1000);\n    return 0;\n}
\n\n

Important: Precision of sleep_for

\n\n

The sleep_for() function does not guarantee absolute precision down to microseconds. The actual wake-up time may be slightly longer (by a few milliseconds) due to the operating system's scheduler. For most tasks (animations, timeouts, sensor polling), this is sufficient. If you need microsecond precision, see the section "When Precision is Critical".

\n\n

The Classic Way: Sleep() for Windows

\n\n

If you are writing code only for Windows (e.g., an old Visual Studio project), you can use the Sleep() function from the Windows API. It sleeps precisely in milliseconds.

\n\n

Example 3: 1 Second Delay on Windows

\n\n
#include <iostream>\n#include <windows.h> // Must include\n\nint main() {\n    std::cout << "Waiting 1 second (Windows)...\\n";\n    Sleep(1000); // Argument in milliseconds\n    std::cout << "Woke up!\\n";\n    return 0;\n}
\n\n

Disadvantages:

\n