std::this_thread::sleep_until
头文件:<thread> (C++11)
template<class Clock, class Duration>
void sleep_until(const std::chrono::time_point<Clock, Duration>& sleep_time);
作用:
阻塞当前正在执行的线程直到sleep_time溢出。
sleep_time是和时钟相关联的,也就是要注意时钟调整会影响到sleep_time。因此,
时钟的时长有可能或没有可能会短于或长于sleep_time。Clock::now()返回调用这个函数时的时间,取决于调整方向。该函数也有可能因为调度或者资源竞争而导致阻塞时间延长到sleep_time溢出之后。
参数:
sleep_time 阻塞时长
返回值:
none
异常:
任何从Clock或Duration抛出的异常(由标准库提供的时钟和时长从来都不会抛出异常)
实例:
#include <iostream>
#include <iomanip>
#include <chrono>
#include <ctime>
#include <thread>
#pragma warning(disable:4996)//加上可去掉unsafe 请使用localtime_s的编译报错
int main()
{
using std::chrono::system_clock;
std::time_t tt = system_clock::to_time_t(system_clock::now());
struct std::tm *ptm = std::localtime(&tt);
std::cout << "Current time: " << std::put_time(ptm, "%X") << '\n'; //必须大写X,若小写x,输出的为日期
std::cout << "Waiting for the next minute to begin...\n";
++ptm->tm_min;
ptm->tm_sec = ;
std::this_thread::sleep_until(system_clock::from_time_t(mktime(ptm)));
std::cout << std::put_time(ptm, "%X") << "reached!\n";
getchar();
return ;
}
结果:
std::this_thread::sleep_until的更多相关文章
- std::this_thread::yield/sleep_for
std::this_thread::yield(): 当前线程放弃执行,操作系统调度另一线程继续执行.. std::this_thread::sleep_for(): 表示当前线程休眠一段时间,休眠期 ...
- android ndk下没有pthread_yield,好在std::this_thread::yield()可以达到同样的效果
一个多线程的算法中,发现线程利用率只有47%左右,大量的处理时间因为usleep(500)而导致线程睡眠: 性能始终上不去. 把usleep(500)修改为std::this_thread::yiel ...
- c++多线程基础2(命名空间 this_thread)
整理自:zh.cppreference.com/w/cpp/thread std::this_thread::yield: 定义于头文件 <thread> 函数原型:void yield( ...
- 第24课 std::thread线程类及传参问题
一. std::thread类 (一)thread类摘要及分析 class thread { // class for observing and managing threads public: c ...
- [原]C++新标准之std::chrono::time_point
原 总结 STL 标准库 chrono time_point ratio 概览 类定义 总结 思考 拓展 system_clock steady_clock high_resolution_cloc ...
- [原]C++新标准之std::chrono::duration
原 总结 C++11 chrono duration ratio 概览 std::chrono::duration 描述 类定义 duration_cast()分析 预定义的duration 示例代 ...
- Game Engine Architecture 3
[Game Engine Architecture 3] 1.Computing performance—typically measured in millions of instructions ...
- C++时间
C++时间 头文件 chrono, 命名空间 std. 现在时间 std::chrono::system_clock::now() 返回系统时钟的当前时间 时钟 std::chrono::system ...
- C/C++ Sleep(0)
Sleep(0) 的意义是放弃当前线程执行的时间片,把自身放到等待队列之中.这时其它的线程就会得到时间片进行程序的程序.Sleep(0)能够降低当前线程的执行速 度,比如:现在系统中有100个线程(先 ...
随机推荐
- 【神经网络与深度学习】GLOG介绍
一.安装配置 1.简介 google 出的一个C++轻量级日志库,支持以下功能: ◆ 参数设置,以命令行参数的方式设置标志参数来控制日志记录行为: ◆ 严重性分级,根据日志严重性分级记录日志: ◆ 可 ...
- Flume入门案例
首先需要通过一个配置文件来配置Agent. 通过flume提供的工具启动agent就可以工作了. (1)编写配置文件 #example.conf:单节点Flume配置 #命名Agent a1的组件 ...
- 记录几个常用的Css样式效果
1.更改字体,图标大小小于12px无效的问题 若我们设置font-size:10px是不会有效果的,需要使用 transform: scale(0.68); 更改字体最小大小 2.设置div边框虚化, ...
- VC/MFC中的CComboBox控件使用详解
CComboBox控件详解 CComboBox控件又称作组合框控件,其有三种形态可供选择,1.简单组合框(Simple)2.下拉组合框(Drop-down)3.下拉列表式组合框(Drop-down l ...
- 云数据库 Redis 版
首先观看视频简介 云数据库 Redis 版是一项易于部署和管理的按需数据库服务,与 Redis 协议兼容.云数据库 Redis 版通过从内存缓存中检索数据而提供高速数据读写功能,并通过同时使用内存和硬 ...
- 那些年,我们见过的 Java 服务端乱象
导读 查尔斯·狄更斯在<双城记>中写道:“这是一个最好的时代,也是一个最坏的时代.” 移动互联网的快速发展,出现了许多新机遇,很多创业者伺机而动:随着行业竞争加剧,互联网红利逐渐消失,很多 ...
- 2018.08.14【2018提高组】模拟A组 比赛总结
题解 这次的A组难得得水. T1 这题我一看,就想起了GDOI的一道题--密码锁 \(O(n)\)算法--差分 于是乎兴奋地发现这道题可以用差分来解. 设\(f_i=a_i-a_{i-1}\). 然后 ...
- Ubuntu18.04 出现E: Sub-process /usr/bin/dpkg returned an error code (100)
You might want to reinstall dpkg by doing the following: sudo -i mkdir /tmp/dpkg cd /tmp/dpkg Mind t ...
- Codeforces 1196D2. RGB Substring (hard version)
传送门 考虑枚举每一个位置作为可能子段的起点,然后对以这个位置为起点的所有情况下的答案取 $min$ 当固定了起点 $i$ 并且固定了起点 $i$ 最终的字符时,答案也固定了 发现对于所有与 $i \ ...
- 前端Ajax通过设置 timeout 参数,轮询后台API
因为我连接的数据库在台湾,相距较远,所以conn.Open()方法打开极慢.前端Ajax访问API时,API的数据还未返回,前端Ajax访问已经超时. 所以设置一个轮询,设置相隔多少秒之后进行一次查询 ...