std::condition_variable
C++ 11 提供了std::condition_variable这个类代表条件变量,与 Linux 系统原生的条件变量一样,同时提供了等待条件变量满足的wait系列方法(wait、wait_for、wait_until 方法),发送条件信号使用notify方法(notify_one和notify_all方法),当然使用std::condition_variable对象时需要绑定一个std::unique_lock或std::lock_guard对象。
C++ 11 中 std::condition_variable 不再需要显式调用方法初始化和销毁。
我们将上文中介绍 Linux 条件变量的例子改写成 C++ 11 版本:
#include <thread> #include <mutex> #include <condition_variable> #include <list> #include <iostream> class Task { public: Task(int taskID) { this->taskID = taskID; } void doTask() { std::cout << "handle a task, taskID: " << taskID << ", threadID: " << std::this_thread::get_id() << std::endl; } private: int taskID; }; std::mutex mymutex; std::list<Task*> tasks; std::condition_variable mycv; void* consumer_thread() { Task* pTask = NULL; while (true) { std::unique_lock<std::mutex> guard(mymutex); while (tasks.empty()) { //如果获得了互斥锁,但是条件不合适的话,pthread_cond_wait会释放锁,不往下执行。 //当发生变化后,条件合适,pthread_cond_wait将直接获得锁。 mycv.wait(guard); } pTask = tasks.front(); tasks.pop_front(); if (pTask == NULL) continue; pTask->doTask(); delete pTask; pTask = NULL; } return NULL; } void* producer_thread() { int taskID = 0; Task* pTask = NULL; while (true) { pTask = new Task(taskID); //使用括号减小guard锁的作用范围 { std::lock_guard<std::mutex> guard(mymutex); tasks.push_back(pTask); std::cout << "produce a task, taskID: " << taskID << ", threadID: " << std::this_thread::get_id() << std::endl; } //释放信号量,通知消费者线程 mycv.notify_one(); taskID ++; //休眠1秒 std::this_thread::sleep_for(std::chrono::seconds(1)); } return NULL; } int main() { //创建5个消费者线程 std::thread consumer1(consumer_thread); std::thread consumer2(consumer_thread); std::thread consumer3(consumer_thread); std::thread consumer4(consumer_thread); std::thread consumer5(consumer_thread); //创建一个生产者线程 std::thread producer(producer_thread); producer.join(); consumer1.join(); consumer2.join(); consumer3.join(); consumer4.join(); consumer5.join(); return 0; }编译并执行程序输出结果如下所示:
[root@localhost testmultithread]# g++ -g -o cpp11cv cpp11cv.cpp -std=c++0x -lpthread [root@localhost testmultithread]# ./cpp11cv produce a task, taskID: 0, threadID: 140427590100736 handle a task, taskID: 0, threadID: 140427623671552 produce a task, taskID: 1, threadID: 140427590100736 handle a task, taskID: 1, threadID: 140427632064256 produce a task, taskID: 2, threadID: 140427590100736 handle a task, taskID: 2, threadID: 140427615278848 produce a task, taskID: 3, threadID: 140427590100736 handle a task, taskID: 3, threadID: 140427606886144 produce a task, taskID: 4, threadID: 140427590100736 handle a task, taskID: 4, threadID: 140427598493440 produce a task, taskID: 5, threadID: 140427590100736 handle a task, taskID: 5, threadID: 140427623671552 produce a task, taskID: 6, threadID: 140427590100736 handle a task, taskID: 6, threadID: 140427632064256 produce a task, taskID: 7, threadID: 140427590100736 handle a task, taskID: 7, threadID: 140427615278848 produce a task, taskID: 8, threadID: 140427590100736 handle a task, taskID: 8, threadID: 140427606886144 produce a task, taskID: 9, threadID: 140427590100736 handle a task, taskID: 9, threadID: 140427598493440 ...更多输出结果省略...需要注意的是,如果在 Linux 平台上,std::condition_variable 也存在虚假唤醒这一现象,如何避免与上文中介绍 Linux 原生的条件变量方法一样。
