I did something similar recently, although mine uses the STL queue. See if you can choose from my implementation. As stated in wilx , you need to wait on condition. My implementation has a maximum limit for items in the queue, and I use this to wait for the mutex / protector to free.
I initially did this on Windows with the option of using Mutex or Critical partitions, so you can specify a template parameter that you can remove and use boost::mutex if that makes it easier for you.
#include <queue>
Thus, you can exchange the queue between the producer / consumer. Usage example
boost::mutex mutex; Queue<boost::mutex> q(mutex, 100); boost::thread_group threads; threads.create_thread(Producer<boost::mutex>(q)); threads.create_thread(Consumer<boost::mutex>(q)); threads.join_all();
With the manufacturer / consumer indicated below
template <typename T> class Producer { public: // Queue passed in explicit Producer(Queue<T> &q) : m_queue(q) {} void operator()() { } }
source share