参考上一篇C++11并发编程
#include <iostream> #include <queue> #include <assert.h> #include <condition_variable> using namespace std; /***经典单消费者,单生产者问题***/ /***货物类,简单的以一个int数据表示货物***/ class Item { public: Item(int i):data(i){} void display(){ cout << "Item: "<<data << endl;} private: int data; }; /***仓库类***/ /***m_CurrentPos 表示当前货物位置,-1表示空仓库***/ /***m_SumSize 表示可以容纳的货物数量***/ /***m_House 用来存放货物(存放Item*)***/ class StoreHouse { public: /***仓库容纳的货物数量***/ StoreHouse(int size):m_SumSize(size),m_CurrentPos(-1) { m_House = new Item *[size]; memset(m_House, 0, size * sizeof(Item *)); } ~StoreHouse() { delete[] m_House; m_House = NULL; } /***仓库是不是满了***/ bool isFull() { return m_CurrentPos == m_SumSize - 1; } /***仓库是不是空的***/ bool isEmpty() { return m_CurrentPos == -1; } /***朝仓库put进一个货物***/ void put(Item *item) { assert((m_CurrentPos +1) != m_SumSize); ++m_CurrentPos; m_House[m_CurrentPos] = item; } /***从仓库get出一个货物***/ Item * get() { assert(m_CurrentPos != -1); Item *item = m_House[m_CurrentPos]; m_House[m_CurrentPos] = NULL; --m_CurrentPos; return item; } private: int m_SumSize; int m_CurrentPos; Item ** m_House; }; StoreHouse gStoreHouse(4);/***仓库有4个货物空位***/ std::mutex mtx; std::condition_variable cv_full; /***仓库满的cv***/ std::condition_variable cv_empty; /***仓库空的cv***/ int toProduceItem = 20;/***总共要生产的货物数量***/ int hasGet = 0; /***生产一个货物***/ Item* produceItem(int itemdata) { Item * item = new Item(itemdata); return item; } /***卖掉一个货物***/ void soldItem(Item *item) { assert(item != NULL); item->display(); delete item; item = NULL; } /***生产者task***/ void producerTask() { for (int i = 0; i < toProduceItem; i++) { std::unique_lock<std::mutex>lck(mtx); while (gStoreHouse.isFull())/***如果仓库满,则通知当前生产者线程等待***/ { cv_full.wait(lck); } Item *item = produceItem(i);/***造个货***/ gStoreHouse.put(item);/***put一个货物进仓库后,现在仓库处于非空仓状态(有货物)***/ cv_empty.notify_one();/***通知某个等待cv_empty的thread,告诉他现在是非空仓状态了(仓库有货,可以来get货了)***/ } cout << "producerTask " << "id: " << " exit" << endl; } /***消费者task***/ void consummerTask() { bool exited = false; while (true) { if (hasGet == toProduceItem) { exited = true; } else { std::unique_lock<std::mutex>lck(mtx); while (gStoreHouse.isEmpty())/***如果仓库空,则通知当前消费者线程等待***/ { cv_empty.wait(lck); } Item *item = gStoreHouse.get();/***get一个货物出来,现在仓库不再满仓***/ ++hasGet; soldItem(item);/***卖掉货物***/ cv_full.notify_one();/***通知某个等待cv_full的thread,告诉他现在不是满仓状态了(仓库没满,可以造货put到仓库了)***/ } if (exited) { break; } } cout << "consummerTask "<<"id: "<<" exit" << endl; } int main() { std::thread producer(producerTask); std::thread consummer0(consummerTask); producer.join(); consummer0.join(); system("pause"); return 0; }