C++

std::condition_variable 条件变量


avatar
GuoYulong 2024-11-14 151

std::condition_variable 条件变量

你可以直接去看 https://en.cppreference.com/w/cpp/thread/condition_variable 的详细解读,当然也可以看下述我进行的翻译和摘要

Defined in header <condition_variable>

std::condition_variable是一种与std::mutex一起使用的同步原语,用于阻止一个或多个线程,直到另一个线程修改共享变量(条件)并通知 std::condition_variable

1.使用条件

情况1: 某个线程打算 修改共享变量
需要完成以下三个条件:

  • 获取std::mutex (通常通过std::lock_guard
  • 在拥有锁的情况下修改共享变量
  • std::condition_variable调用notify_onenotify_all (可以在释放锁后完成)

情况2: 某线程想要等待条件变量
需要完成以下两个条件:

  • 获取用于保护共享变量的互斥锁上的std:: unique_lock < std:: mutex >
  • 执行以下操作之一:
    • 检查情况,是否已更新并通知
    • 对std::condition_variable调用wait 、 wait_for或wait_until (以原子方式释放互斥体并挂起线程执行,直到通知条件变量、超时到期或发生虚假唤醒,然后在返回之前以原子方式获取互斥体)
    • 检查条件,如果不满足则继续等待。
  • 或者:
    • 使用wait 、 wait_for和wait_until的谓词重载,它执行相同的三个步骤

2.Function

Notification 通知:

  • notify_one:通知一个等待线程
  • notify_all:通知所有等待线程

Waiting 等待:

  • wait:阻塞当前线程,直到条件变量被唤醒
  • wait_for:阻塞当前线程,直到条件变量被唤醒或指定的超时时间后
  • wait_until:阻塞当前线程,直到条件变量被唤醒或到达指定时间点

3.例程

#include <condition_variable>
#include <iostream>
#include <mutex>
#include <string>
#include <thread>

std::mutex m;
std::condition_variable cv;
std::string data;
bool ready = false;
bool processed = false;

void worker_thread()
{
    // wait until main() sends data
    std::unique_lock lk(m);
    cv.wait(lk, []{ return ready; });

    // after the wait, we own the lock
    std::cout << "Worker thread is processing data\n";
    data += " after processing";

    // send data back to main()
    processed = true;
    std::cout << "Worker thread signals data processing completed\n";

    // manual unlocking is done before notifying, to avoid waking up
    // the waiting thread only to block again (see notify_one for details)
    lk.unlock();
    cv.notify_one();
}

int main()
{
    std::thread worker(worker_thread);

    data = "Example data";
    // send data to the worker thread
    {
        std::lock_guard lk(m);
        ready = true;
        std::cout << "main() signals data ready for processing\n";
    }
    cv.notify_one();

    // wait for the worker
    {
        std::unique_lock lk(m);
        cv.wait(lk, []{ return processed; });
    }
    std::cout << "Back in main(), data = " << data << '\n';

    worker.join();
}

输出结果:

main() signals data ready for processing
Worker thread is processing data
Worker thread signals data processing completed
Back in main(), data = Example data after processing

相关阅读

注意!!!

新增会员中心页面,方便管理个人账户,充值功能暂不开启,请勿为本网站进行任何充值活动!!!

通知!!!

① 过年好!!!拖更几个月了已经,年后继续更新!!