std::this_thread::yield()使用理解

摘选 stackoverflow 一段: 
http://stackoverflow.com/questions/11048946/stdthis-threadyield-vs-stdthis-threadsleep-for

std::this_thread::yield tells the implementation to reschedule the execution of threads, 
that should be used in a case where you are in a busy waiting state, like in a thread pool: 

while(true) {
  if(pool.try_get_work()) {
    // do work
  }
  else {
    std::this_thread::yield(); // other threads can push work to the queue now
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

std::this_thread::sleep_for can be used if you really want to wait for a specific amount of time. 
This can be used for task, where timing really matters, e.g.: if you really only want to wait for 
2 seconds. (Note that the implementation might wait longer than the given time duration)

个人理解: 
(1) std::this_thread::yield(); 是将当前线程所抢到的CPU”时间片A”让渡给其他线程(其他线程会争抢”时间片A”, 
注意: 此时”当前线程”不参与争抢). 
等到其他线程使用完”时间片A”后, 再由操作系统调度, 当前线程再和其他线程一起开始抢CPU时间片.

(2) 如果将 std::this_thread::yield();上述语句修改为: return; ,则将未使用完的CPU”时间片A”还给操作系统, 再由操作系统调度, 当前线程和其他线程一起开始抢CPU时间片.

std::this_thread::yield(): 适合应用在 — “that should be used in a case where you are in a busy waiting state”, 如果存在此情况, 线程实际的执行代码类似如下:

void thread_func()
{
    while (1)
    {
        if (!HasTask())
        {
            return;
        }
       // else
       // {
       //     // do work
       // }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

因为条件不满足, 所以此线程很快就返回(然后立即与其他线程竞争CPU时间片), 结果就是此线程频繁的与其他线程争抢CPU时间片, 从而影响程序性能,使用 std::this_thread::yield() 后, 就相当于”当前线程”检查条件不成功后, 将其未使用完的”CPU时间片”分享给其他线程使用, 等到其他线程用完后, 再和其他线程一起竞争.

结论: 
std::this_thread::yield() 的目的是避免一个线程(that should be used in a case where you are in a busy waiting state)频繁与其他线程争抢CPU时间片, 从而导致多线程处理性能下降.

std::this_thread::yield() 是让当前线程让渡出自己的CPU时间片(给其他线程使用) 
std::this_thread::sleep_for() 是让当前休眠”指定的一段”时间.

sleep_for()也可以起到 std::this_thread::yield()相似的作用, (即:当前线程在休眠期间, 自然不会与其他线程争抢CPU时间片)但两者的使用目的是大不相同的: 
std::this_thread::yield() 是让线程让渡出自己的CPU时间片(给其他线程使用) 
sleep_for() 是线程根据某种需要, 需要等待若干时间.

参考文章: 
http://stackoverflow.com/questions/11048657/giving-up-the-time-slice-for-current-thread-in-c 
http://stackoverflow.com/questions/11048946/stdthis-threadyield-vs-stdthis-threadsleep-for

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值