| CARVIEW |
Select Language
HTTP/2 200
date: Tue, 30 Dec 2025 11:39:12 GMT
content-type: text/html; charset=utf-8
server: cloudflare
last-modified: Mon, 05 Jul 2021 23:22:32 GMT
vary: Accept-Encoding
access-control-allow-origin: *
expires: Tue, 30 Dec 2025 11:49:12 GMT
cache-control: max-age=600
x-proxy-cache: MISS
x-github-request-id: 2D53:C06A:5F5D939:66AF291:6953B9E0
nel: {"report_to":"cf-nel","success_fraction":0.0,"max_age":604800}
report-to: {"group":"cf-nel","max_age":604800,"endpoints":[{"url":"https://a.nel.cloudflare.com/report/v4?s=id%2FRhkOqonbHadZC3Tl0etbE7zBrL4sYp81PoAb3TH7DM7LxVlMjlLDbsxcfRY%2F1wHe2b2ZYRBbQun1v0qlqrEDYGYpr3vfrvARRekrpzQ%3D%3D"}]}
cf-cache-status: DYNAMIC
content-encoding: gzip
cf-ray: 9b61415b3ac097f6-BOM
alt-svc: h3=":443"; ma=86400
Sleep - C++ Patterns
← Patterns
Sleep
123456789101112 | #include <chrono>
#include <thread>
using namespace std::literals::chrono_literals;
int main()
{
std::chrono::milliseconds sleepDuration(20);
std::this_thread::sleep_for(sleepDuration);
std::this_thread::sleep_for(5s);
} |
This pattern is licensed under the CC0 Public Domain Dedication.
Requires
c++11
or newer.
Intent
Block the execution of a thread for a given amount of time.
Description
On line 7, we create a std::chrono::milliseconds
object representing the number of milliseconds to sleep (other
duration units may also be used). On line 8, the call to
std::this_thread::sleep_for will block
the execution of the current thread for at least the given
duration.
On line 10, we demonstrate the use of C++14’s duration literal
suffixes
by representing a duration with the seconds suffix, s. The
using directive on line 4 is required in order to use these suffixes.