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

本文对比了C++中std::this_thread::yield与std::this_thread::sleep_for的区别与应用场景。前者用于线程让渡CPU时间片,减少忙等状态下的资源浪费;后者使线程休眠特定时间,适用于对定时要求严格的任务。

摘选 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
  }
}

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
       // }
    }
}

因为条件不满足, 所以此线程很快就返回(然后立即与其他线程竞争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

### `std::this_thread::yield()` 的作用 `std::this_thread::yield()` 是 C++11 标准中 `<thread>` 头文件提供的一个函数,用于让当前正在执行的线程主动放弃当前的 CPU 时间片,使其他处于就绪状态的线程有机会获得 CPU 资源并运行。调用该函数后,当前线程会被重新放入调度队列中,等待调度器的下一次调度[^1]。 与 `std::this_thread::sleep_for()` 不同,`yield()` 并不会使线程进入休眠状态,而是立即释放 CPU,适用于需要主动让出资源但不希望阻塞线程的场景[^3]。 ### `std::this_thread::yield()` 的使用方法 该函数没有参数,也无返回值,调用方式简单,直接使用 `std::this_thread::yield()` 即可。以下是一个示例: ```cpp #include <iostream> #include <thread> void thread_task() { for (int i = 0; i < 5; ++i) { std::cout << "子线程运行中..." << std::endl; std::this_thread::yield(); // 主动让出 CPU } } int main() { std::thread t(thread_task); for (int i = 0; i < 5; ++i) { std::cout << "主线程运行中..." << std::endl; std::this_thread::yield(); // 主线程让出 CPU } t.join(); return 0; } ``` 在上述代码中,主线程和子线程交替执行,每次打印信息后调用 `std::this_thread::yield()`,以主动释放 CPU 资源,使得对方线程有机会运行。 ### 使用场景 - **线程协作**:在多个线程之间进行协作时,某个线程完成阶段性任务后可以主动让出 CPU,让其他线程继续执行。 - **避免忙等待**:在忙等待(busy waiting)循环中,可以插入 `yield()` 以减少 CPU 资源的占用。 - **公平调度**:在某些调度算法中,为了保证多个线程的公平执行,可以周期性地调用 `yield()`。 需要注意的是,`yield()` 的行为依赖于操作系统的调度策略,并不能保证其他线程一定会立即执行,仅表示当前线程愿意让出 CPU[^2]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值