首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::condition_variable

Defined in header <condition_variable>

?

?

class condition_variable;

?

(since C++11)

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

要修改变量的线程必须修改。

  1. 获得std::mutex%28std::lock_guard%29
  1. 在持有锁时执行修改。
  1. 执行notify_onenotify_allstd::condition_variable%28不需要保存锁以获得通知%29

即使共享变量是原子变量,也必须在互斥项下修改它,以便正确地将修改发布到等待线程。

任何打算等待的线程std::condition_variable一定要。

  1. 获得std::unique_lock<std::mutex>,与用于保护共享变量的互斥对象相同。
  1. 执行wait,,,wait_for,或wait_until等待操作原子地释放互斥体并暂停线程的执行。
  1. 通知条件变量时,超时过期,或假唤醒发生时,线程被唤醒,互斥体被原子地重新获取。然后,线程应该检查条件,如果唤醒是假的,则继续等待。

std::condition_variable只适用于std::unique_lock<std::mutex>这个限制允许在某些平台上实现最大的效率。std::condition_variable_any提供一个条件变量,该变量可用于任何BasicLockable对象,例如std::shared_lock...

条件变量允许并发调用wait,,,wait_for,,,wait_until,,,notify_onenotify_all成员职能。

全班std::condition_variableStandardLayoutType不是CopyConstructible,,,MoveConstructible,,,CopyAssignable,,,MoveAssignable...

成员类型

Member type

Definition

native_handle_type

implementation-defined

成员函数

(constructor)

constructs the object (public member function)

(destructor)

destructs the object (public member function)

operator= deleted

not copy-assignable (public member function)

通知

通知[医]通知一个等待线程%28公共成员函数%29

通知[医]所有通知所有等待线程%28公共成员函数%29

等待

等待阻止当前线程,直到唤醒条件变量%28公共成员函数%29

等待[医]对于阻塞当前线程,直到条件变量被唤醒为止,或者在指定的超时持续时间%28公共成员函数%29之后。

等待[医]直到阻塞当前线程,直到条件变量被唤醒,或者直到达到指定的时间点为止,才达到%28公共成员函数%29。

本机手柄

土生土长[医]句柄返回本机句柄%28公共成员函数%29

condition_variablestd::mutex以方便线程间的通信。

二次

代码语言:javascript
复制
#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();
}

二次

产出:

二次

代码语言:javascript
复制
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

二次

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com