条件变量std::condition_variable

https://en.cppreference.com/w/cpp/thread/condition_variable

头文件

<condition_variable>

声明

class condition_variable;(since C++11)

描述

condition_variable类是一个同步原语,可用于阻塞一个线程或同时阻塞多个线程,直到另一个线程修改共享变量(条件)并通知condition_variable。

尝试修改共享变量的线程必须:

  1. 获取一个std :: mutex(通常通过std :: lock_guard).
  2. 持有该锁的线程执行修改.
  3. 在std :: condition_variable上执行notify_one或notify_all(该锁不需要保留以进行通知).

即使共享变量是原子变量,也必须在互斥量的保护下对其进行修改,以便正确地将修改发布到等待的线程

任何在std :: condition_variable上等待的线程都必须

  1. 获取std :: unique_lock <std :: mutex>,用同一个互斥锁来保护共享变量.
  2. 或者
    1. 执行wait,wait_for或wait_until。等待操作以原子方式释放互斥锁并挂起当前线程的执行。
    2. 条件变量被通知,超时到期或发生虚假唤醒时,将唤醒线程,并以原子方式重新获取互斥量。然后,线程检查条件变量,如果唤醒是虚假的,则应继续等待。

std :: condition_variable只能与std :: unique_lock <std :: mutex>配合使用;此限制允许在某些平台上获得最大效率。 std :: condition_variable_any提供了可与任何BasicLockable对象一起使用的条件变量,例如std :: shared_lock。

条件变量通过notify_one和notify_all成员函数允许并发唤醒wait、wait_for、wait_until。

std::condition_variable 类是StandardLayoutType类型, 它不能 CopyConstructible, MoveConstructible, CopyAssignable, or MoveAssignable.

成员

Member types

Member typeDefinition
native_handle_typeimplementation-defined

Member functions

(constructor)

constructs the object
(public member function)

(destructor)

destructs the object
(public member function)

operator=

[deleted]

not copy-assignable
(public member function)

Notification

notify_one

notifies one waiting thread
(public member function)

notify_all

notifies all waiting threads
(public member function)

Waiting

wait

blocks the current thread until the condition variable is woken up
(public member function)

wait_for

blocks the current thread until the condition variable is woken up or after the specified timeout duration
(public member function)

wait_until

blocks the current thread until the condition variable is woken up or until specified time point has been reached
(public member function)

Native handle

native_handle

returns the native handle
(public member function)

举例

condition_variable 跟 std::mutex结合使用,这样就能实现线程间的交互。

#include <iostream>
#include <string>
#include <thread>
#include <mutex>
#include <condition_variable>
 
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<std::mutex> 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<std::mutex> lk(m);
        ready = true;
        std::cout << "main() signals data ready for processing\n";
    }
    cv.notify_one();
 
    // wait for the worker
    {
        std::unique_lock<std::mutex> 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

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值