std::this_thread::yield/sleep_for
std::this_thread::yield(): 当前线程放弃执行,操作系统调度另一线程继续执行。。
std::this_thread::sleep_for(): 表示当前线程休眠一段时间,休眠期间不与其他线程竞争CPU,根据线程需求,等待若干时间。
#include <iostream>
#include <chrono>
#include <thread> void little_sleep(std::chrono::microseconds us)
{
auto start = std::chrono::high_resolution_clock::now();
auto end = start + us;
do {
std::this_thread::yield();
} while (std::chrono::high_resolution_clock::now() < end);
} int main()
{
auto start = std::chrono::high_resolution_clock::now(); little_sleep(std::chrono::microseconds()); auto elapsed = std::chrono::high_resolution_clock::now() - start;
std::cout << "waited for "
<< std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count()
<< " microseconds\n"; system("pause");
}
std::this_thread::yield/sleep_for的更多相关文章
- android ndk下没有pthread_yield,好在std::this_thread::yield()可以达到同样的效果
一个多线程的算法中,发现线程利用率只有47%左右,大量的处理时间因为usleep(500)而导致线程睡眠: 性能始终上不去. 把usleep(500)修改为std::this_thread::yiel ...
- std::this_thread::sleep_until
头文件:<thread> (C++11) template<class Clock, class Duration> void sleep_u ...
- C++11 并发指南------std::thread 详解
参考: https://github.com/forhappy/Cplusplus-Concurrency-In-Practice/blob/master/zh/chapter3-Thread/Int ...
- C++11并发——多线程条件变量std::condition_variable(四)
https://www.jianshu.com/p/a31d4fb5594f https://blog.csdn.net/y396397735/article/details/81272752 htt ...
- c++多线程基础2(命名空间 this_thread)
整理自:zh.cppreference.com/w/cpp/thread std::this_thread::yield: 定义于头文件 <thread> 函数原型:void yield( ...
- 第31课 std::atomic原子变量
一. std::atomic_flag和std::atomic (一)std::atomic_flag 1. std::atomic_flag是一个bool类型的原子变量,它有两个状态set和clea ...
- 第24课 std::thread线程类及传参问题
一. std::thread类 (一)thread类摘要及分析 class thread { // class for observing and managing threads public: c ...
- C++20 多线程 std::jthread
在C++20中新加了jthread类,jthread是对thread的一种封装 std::jthread 构造函数 (1)jthread() noexcept; (2)jthread( jthread ...
- C++11 并发指南六( <atomic> 类型详解二 std::atomic )
C++11 并发指南六(atomic 类型详解一 atomic_flag 介绍) 一文介绍了 C++11 中最简单的原子类型 std::atomic_flag,但是 std::atomic_flag ...
随机推荐
- 初学FPGA-IP核错误
[BD 5-336] This command cannot be run, as the BD-design is locked. Locked reason(s):* BD design cont ...
- python自增自减?赋值语句返回值?逗号表达式?
咳咳,直接进入正题吧. 自增自减(++/--),以及赋值语句,还有逗号表达式都是在C/C++中常见的运算符或表达式. 熟悉C/C++的小伙伴们都知道,在C/C++中: 自增自减(前缀/后缀)运算符将实 ...
- 使用 Jest 进行 Vue 单元测试
本文介绍:1.vue-cli3下jest环境的搭建2.vue组件基本的测试方法 环境配置 vue-cli3 的插件使安装流程变得格外简单,通过 vue ui 启动可视化管理系统,在插件栏,点击 ‘添加 ...
- centos 升级
yum -y update升级所有包同时也升级软件和系统内核 yum -y upgrade只升级所有包,不升级软件和系统内核
- FormatMessage将错误代码转换成对应的字符串
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" int _tmain(int argc, _T ...
- windows下tomcat启动日志乱码
在windows下用startup.bat启动时,控制台里显示乱码,如图: 解决方案: 修改conf文件下的logging.properties文件,将控制台输出的编码修改为GBK: java.uti ...
- 前端深入之css篇|你真的了解“权重”吗?
写在前面 权重这个概念,相信对许多进行过前端开发的小伙伴来说肯定并不陌生,有时候一个样式添加不上,我们就会一个 !important 怼上去,一切就好像迎刃而解了.但还有的时候,!important也 ...
- php7和php5区别是什么
PHP7距正式发布以及有挺长时间了,刚出道就号称比旧版本快了几倍,各种开源框架或系统运行在PHP7上速度效率提高了几倍.那么php7和php5之间的区别是什么?下面本篇文章就来给大家简单介绍一下,希望 ...
- ActiveMQ学习总结------入门篇01
注:*这篇博文文章主要介绍ActiveMQ是什么原理性的内容和如何安装和简易操作 一. ActiveMQ 简介 1 ActiveMQ是什么呢?看起来好碉堡的东西哇! ActiveMQ 是 Apach ...
- C++学习笔记二、头文件与源文件
头文件 .h 与源文件 .ccp 的区别 .h 文件一般是用来定义的,比如定义函数.类.结构体等: .cpp 文件则是对头文件的定义进行实现. include .h文件,可以调用你声明的函数.类等.当 ...