C++11 条件变量

条件变量是另外一种用于等待的同步机制,它能阻塞一个或多个线程,直到收到另一个线程发出的通知或者超时才会唤醒当前阻塞的线程

条件变量需要和互斥量配合使用

C++11 头文件 <condition_variable > 中包含了与条件变量想关的类和函数:

    • std::condition_variable
    • std::condition_variable_any
  • 枚举
    • cv_status
  • 函数
    • notify_all_at_thread_exit

condition_variable

当 std::condition_variable 对象的某个等待函数被调用的时候,它使用 std::unique_lockstd::mutex 来锁住当前线程。当前线程会一直被阻塞,直到另外一个线程在相同的 std::condition_variable 对象上调用了通知函数来唤醒当前线程。

std::condition_variable 对象通常使用 std::unique_lockstd::mutex 来等待,如果需要使用另外的 lockable 类型,可以使用后面激昂会介绍的 std::condition_variable_any 类。

  • 成员函数

    // 构造析构
    (constructor)               // 只提供了默认构造函数
    (destructor)
    
    // 等待函数
    wait 
    wait_for
    wait_until
    
    // 通知函数
    notify_one                  // 唤醒一个因该条件变量阻塞的线程
    notify_all                  // 唤醒所有因该条件变量阻塞的线程
    
    • wait_* 方法需要传入一个 std::unique_lock 对象。当线程中条件变量调用 wait 方法之后当前线程将会被阻塞,同时会调用 std::unique_lock::unlock 方法释放锁。当线程获得通知被唤醒时自动调用 std::unique_lock::lock 上锁。
    • wait_* 方法另一个版本需要追加一个参数,该参数是一个返回值为布尔类型的可调用对象,当它返回 false 时调用 wait 会阻塞线程,返回 true 时调用 wait 不会阻塞线程。相当于:
      while (!callableObject()) wait(lck);
      
    • notify_one 通知一个被条件变量阻塞的进程,如果没有因为该条件变量进入等待的线程则什么也不做,如果有一个则唤醒该线程,如果有多个线程在等待,那么不能保证具体唤醒哪一个线程
  • 应用实例

    /***************************************************************
     * (constructor)
     * wait
     * notify_all
     * ************************************************************/
    std::mutex mtx01;
    std::condition_variable cv01;
    
    bool runFlag = false;
    
    void func01(int index) 
    {
        std::unique_lock <std::mutex> ulck(mtx01);
        
        if (!runFlag) cv01.wait(ulck);
    
        std::cout << index << ": thread id " << std::this_thread::get_id() << std::endl;
    }
    
    void runThreads()
    {
        std::lock_guard<std::mutex> glck(mtx01);
    
        runFlag = true;
    
        cv01.notify_all();
    }
    
    std::vector<std::thread> thVec(2);
    
    for (int i = 0; i < 2; ++i)
    {
        thVec[i] = std::move(std::thread(func01, i + 1));
    }
    
    std::this_thread::sleep_for(std::chrono::milliseconds(500));
    
    runThreads();
    
    for (auto& th : thVec)
    {
        th.join();
    }
    /*
    // ---------------------------------------------------------
    // output
    // ---------------------------------------------------------
    2: thread id 16868
    1: thread id 28712
    */
    

condition_variable_any

std::condition_variable_any 与 std::condition_variable 类似,只不过 std::condition_variable_any 的 wait 函数可以接受任何 lockable 参数,而 std::condition_variable 只能接受 std::unique_lockstd::mutex 类型的参数。 除此以外两者几乎完全一样。

cv_status

cv_status::no_timeout       // wait_for 或者 wait_until 没有超时,即在规定的时间段内线程收到了通知
cv_status::timeout          // wait_for 或者 wait_until 超时

notify_all_at_thread_exit

当条件变量调用 notify_all_at_thread_exit 函数的线程退出时,所有在该条件变量上等待的线程都会收到通知。

notify_all_at_thread_exit 的函数原型如下:

void notify_all_at_thread_exit (condition_variable& cond, unique_lock<mutex> lck);

应用实例:

std::mutex mtx01;
std::condition_variable cv01;

bool runFlag = false;

void func01(int index) 
{
    std::unique_lock <std::mutex> ulck(mtx01);
    
    if (!runFlag) cv01.wait(ulck);

    std::cout << index << ": thread id " << std::this_thread::get_id() << std::endl;
}

void runThreads()
{
    std::unique_lock<std::mutex> ulck(mtx01);

    std::notify_all_at_thread_exit(cv01, std::move(ulck));
}

int main()
{
    std::vector<std::thread> thVec(10);

    for (int i = 0; i < 10; ++i)
    {
        thVec[i] = std::move(std::thread(func01, i + 1));
    }

    std::this_thread::sleep_for(std::chrono::milliseconds(500));

    std::thread(runThreads).detach();

    for (auto& th : thVec)
    {
        th.join();
    }
}

/*
// ---------------------------------------------------------
// output
// ---------------------------------------------------------
8: thread id 36480
7: thread id 10476
4: thread id 42556
2: thread id 37532
10: thread id 14644
6: thread id 11304
3: thread id 26508
9: thread id 21116
5: thread id 44584
1: thread id 24276
*/

C++11 多线程并发编程目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值