首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::this_thread::sleep_for

Defined in header <thread>

?

?

template< class Rep, class Period > void sleep_for( const std::chrono::duration<Rep, Period>& sleep_duration );

?

(since C++11)

阻止当前线程的执行。至少指定sleep_duration...

用一个稳定的时钟来测量持续时间。此函数的阻塞时间可能超过sleep_duration由于调度或资源争用延迟。

参数

sleep_duration

-

time duration to sleep

返回值

%280%29

例外

任何由时钟、时间引发的异常[医]在执行%28时钟、时间点和标准库提供的持续时间期间,不要抛出%29。

二次

代码语言:javascript
复制
#include <iostream>
#include <chrono>
#include <thread>
 
int main()
{
    using namespace std::chrono_literals;
    std::cout << "Hello waiter" << std::endl;
    auto start = std::chrono::high_resolution_clock::now();
    std::this_thread::sleep_for(2s);
    auto end = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double, std::milli> elapsed = end-start;
    std::cout << "Waited " << elapsed.count() << " ms\n";
}

二次

可能的产出:

二次

代码语言:javascript
复制
Hello waiter
Waited 2000.12 ms

二次

另见

sleep_until (C++11)

stops the execution of the current thread until a specified time point (function)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com