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的更多相关文章

  1. android ndk下没有pthread_yield,好在std::this_thread::yield()可以达到同样的效果

    一个多线程的算法中,发现线程利用率只有47%左右,大量的处理时间因为usleep(500)而导致线程睡眠: 性能始终上不去. 把usleep(500)修改为std::this_thread::yiel ...

  2. std::this_thread::sleep_until

    头文件:<thread>                  (C++11) template<class Clock, class Duration> void sleep_u ...

  3. C++11 并发指南------std::thread 详解

    参考: https://github.com/forhappy/Cplusplus-Concurrency-In-Practice/blob/master/zh/chapter3-Thread/Int ...

  4. C++11并发——多线程条件变量std::condition_variable(四)

    https://www.jianshu.com/p/a31d4fb5594f https://blog.csdn.net/y396397735/article/details/81272752 htt ...

  5. c++多线程基础2(命名空间 this_thread)

    整理自:zh.cppreference.com/w/cpp/thread std::this_thread::yield: 定义于头文件 <thread> 函数原型:void yield( ...

  6. 第31课 std::atomic原子变量

    一. std::atomic_flag和std::atomic (一)std::atomic_flag 1. std::atomic_flag是一个bool类型的原子变量,它有两个状态set和clea ...

  7. 第24课 std::thread线程类及传参问题

    一. std::thread类 (一)thread类摘要及分析 class thread { // class for observing and managing threads public: c ...

  8. C++20 多线程 std::jthread

    在C++20中新加了jthread类,jthread是对thread的一种封装 std::jthread 构造函数 (1)jthread() noexcept; (2)jthread( jthread ...

  9. C++11 并发指南六( <atomic> 类型详解二 std::atomic )

    C++11 并发指南六(atomic 类型详解一 atomic_flag 介绍)  一文介绍了 C++11 中最简单的原子类型 std::atomic_flag,但是 std::atomic_flag ...

随机推荐

  1. Egiht(八种方法)

    Problem Description The 15-puzzle has been around for over 100 years; even if you don't know it by t ...

  2. thinkphp6 常用方法文档

    请求变量 use think\facade\Request; Request::param('name'); Request::param();全部请求变量 返回数组 Request::param([ ...

  3. c++异常处理函数

    注意: throw 抛出异常,catch 捕获异常,try 尝试捕获异常 catch 中的参数类型要和throw 抛出的数据类型一致 try{    //可能抛出异常的语句}catch (异常类型1) ...

  4. 03: OpenGL ES 基础教程02 使用OpenGL ES 基本步骤

    第二章:让硬件为你工作(OpenGL ES 应用实践指南 iOS卷) 前言: 1:使用OpenGL ES 基本步骤 2:绘制三角形 3:效果 正文: 一:使用OpenGL ES 基本步骤 1:生成缓存 ...

  5. 40 (OC)* 数据库常见sql语句

    1:增加INSERT INTO t_student (name, age) VALUES ('liwx', 18);2:删除DELETE FROM t_student WHERE name = 'li ...

  6. docker安装centos6

    1,获取Centos镜像>docker pull centos:centos6 2,查看镜像运行情况>docker images centos 3,在容器下运行 shell bash> ...

  7. [Spark] 00 - Install Hadoop & Spark

    Hadoop安装 Java环境配置 安装课程:安装配置 配置手册:Hadoop安装教程_单机/伪分布式配置_Hadoop2.6.0/Ubuntu14.04[依照步骤完成配置] jsk安装使用的链接中第 ...

  8. maven 项目报错org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)解决

    idea在使用maven构建的项目中使用mybatis时报错org.apache.ibatis.binding.BindingException: Invalid bound statement (n ...

  9. mybatis 常用的jabcType与javaType对应

    一.jabcType与javaType对应   JDBC Type            Java Type CHAR                 String VARCHAR           ...

  10. js 指定分隔符连接数组元素join()

    示例:<script type="text/javascript"> var myarr = new Array(3); myarr[0] = "I" ...