Home >Backend Development >C++ >How Can I Pause a C Thread's Execution for a Specific Number of Milliseconds?
In C , the POSIX sleep() function is available for suspending a thread's execution for a specified duration. However, this function counts time in seconds. For finer control over thread sleep times, C 11 introduced a solution.
In C 11, the
To make a program sleep for x milliseconds, follow these steps:
Include Necessary Headers:
#include <chrono> #include <thread>
Use std::this_thread::sleep_for():
std::this_thread::sleep_for(std::chrono::milliseconds(x));
This line of code will suspend the current thread's execution for x milliseconds. The std::chrono::milliseconds type wraps around an integer representing the number of milliseconds.
Compared to using the POSIX sleep() function, the
The above is the detailed content of How Can I Pause a C Thread's Execution for a Specific Number of Milliseconds?. For more information, please follow other related articles on the PHP Chinese website!