c++ 同步阻塞队列
参考:《C++11深入应用》
用同步阻塞队列解决生产者消费者问题。
生产者消费者问题:
有一个生产者在生产产品,这些产品将提供给若干个消费者去消费,为了使生产者和消费者能并发执行,在两者之间设置一个有多个缓冲区的缓冲池,
生产者将它生产的产品放入一个缓冲区中,消费者可以从缓冲区中取走产品进行消费,所有生产者和消费者都是异步方式运行的,但它们必须保持同步,
即不允许消费者到一个空的缓冲区中取产品,也不允许生产者向一个已经装满产品且尚未被取走的缓冲区中投放产品。
//SyncQueue.hpp #include <list> #include <mutex> #include <condition_variable> #include <iostream> template<typename T> class SyncQueue { private: bool IsFull() const { return m_queue.size() == m_maxSize; } bool IsEmpty() const { return m_queue.empty(); } public: SyncQueue(int maxSize) : m_maxSize(maxSize) { } void Put(const T& x) { std::lock_guard<std::mutex> locker(m_mutex); while (IsFull()) { std::cout << "the blocking queue is full,waiting..." << std::endl; m_notFull.wait(m_mutex); } m_queue.push_back(x); m_notEmpty.notify_one(); } void Take(T& x) { std::lock_guard<std::mutex> locker(m_mutex); while (IsEmpty()) { std::cout << "the blocking queue is empty,wating..." << std::endl; m_notEmpty.wait(m_mutex); } x = m_queue.front(); m_queue.pop_front(); m_notFull.notify_one(); } private: std::list<T> m_queue; //缓冲区 std::mutex m_mutex; //互斥量和条件变量结合起来使用 std::condition_variable_any m_notEmpty;//不为空的条件变量 std::condition_variable_any m_notFull; //没有满的条件变量 int m_maxSize; //同步队列最大的size };
测试:
//test.cpp #include "SyncQueue.hpp" #include <thread> #include <iostream> #include <chrono> SyncQueue<int> syncQueue(5); void Produce() { for (int i = 0; i < 15; ++i) { std::this_thread::sleep_for(std::chrono::seconds(1)); syncQueue.Put(888); std::cout<<"put(888)"<<std::endl; } } void Consume() { int x = 0; for (int i = 0; i < 5; ++i) { std::this_thread::sleep_for(std::chrono::seconds(1)); syncQueue.Take(x); std::cout << "take(888)" << std::endl; } } int main(void) { std::thread producer(Produce); std::thread consumer1(Consume); std::thread consumer2(Consume); std::thread consumer3(Consume); producer.join(); consumer1.join(); consumer2.join(); consumer3.join(); return 0; }