I figured it out. Silly mistake on my part, I was not actually deleting the element from the queue, I was just reading the first element. I modified the code and the code below no works. Thanks all for the help.
I'm trying to implement the producer consumer problem using boost, which is actually part of a bigger project. I have implemented a program from examples on the internet and even some help I found here. However currently my code just hangs. Based on some good advice, I decided to use the boost ciruclar buffer to hold my data between the producer and consumer. Lots of similar code out there and I was able to pool ideas from those and write something on my own. However, I still seem to be having same problem as before (which is my program just hangs). I thought that I did not make the same mistake as before..
My code is given below, I have taken out my earlier code where I just my own singly link list.
Buffer header:
#ifndef PCDBUFFER_H
#define PCDBUFFER_H
#include <pcl/io/pcd_io.h>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition.hpp>
#include <boost/circular_buffer.hpp>
class pcdBuffer
{
public:
pcdBuffer(int buffSize);
void put(int data);
int get();
bool isFull();
bool isEmpty();
int getSize();
int getCapacity();
private:
boost::mutex bmutex;
boost::condition_variable buffEmpty;
boost::condition_variable buffFull;
boost::circular_buffer<int> buffer;
};
#endif
Buffer source (only relevant parts):
#include "pcdBuffer.h"
#include <iostream>
//boost::mutex io_mutex;
pcdBuffer::pcdBuffer(int buffSize)
{
buffer.set_capacity(buffSize);
}
void pcdBuffer::put(int data)
{
{
boost::mutex::scoped_lock buffLock(bmutex);
while(buffer.full())
{
std::cout << "Buffer is full" << std::endl;
buffFull.wait(buffLock);
}
buffer.push_back(data);
}
buffEmpty.notify_one();
}
int pcdBuffer::get()
{
int data;
{
boost::mutex::scoped_lock buffLock(bmutex);
while(buffer.empty())
{
std::cout << "Buffer is empty" << std::endl;
buffEmpty.wait(buffLock);
}
data = buffer.front();
buffer.pop_front();
}
buffFull.notify_one();
return data;
}
main driver for the code:
#include <iostream>
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>
#include <unistd.h>
#include "pcdBuffer.h"
pcdBuffer buff(100);
void producer()
{
int i = 10;
while (true)
{
buff.put(i);
i++;
}
}
void consumer()
{
int i;
while(true)
{
i = buff.get();
std::cout << "Data: " << i << std::endl;
}
}
int main(int argc, char** argv)
{
std::cout << "Starting main...." << std::endl;
std::cout << "Buffer Details: " << std::endl;
std::cout << "Capacity: " << buff.getCapacity() << ", isEmpty: " << buff.isEmpty() << ", isFull: " << buff.isFull() << std::endl;
boost::thread cons(consumer);
sleep(5);
boost::thread prod(producer);
prod.join();
cons.join();
return 0;
}
My buffer capacity is correctly initialized to 100. The consumer thread waits and reports that the "buffer is empty" for the 5 seconds, but after that I just get the "buffer is full" from the put method and "Data : 10" from the consumer function alternating on stdout. As you can see 10 is the first element that I put in. It seems that the buffer is getting filled up and not notifiying the consumer but I checked my locks, and think they are right. Any help on this is greatly appreciated.
Here are a link of references from which I wrote this code:
http://www.drdobbs.com/article/print?articleId=184401518&siteSectionName=cpp
First of all, instead of writing your own list, you could just wrap std::list
in pcdQueue
instead of writing your own. It is correct, that std::list
is not thread-safe as-is, but you are providing the necessary synchronisation primitives in your class anyway.
The reason why your program hangs:
You keep the lock and fill the queue until it is full. Your notificaiton of the consumer via notify_one
is useless, because your consumer will lock right again, since the mutex is already taken (by the lock in the producer).
When you finally release the lock (when the queue is full) by waiting on the condition_variable
, you don't wake up your consumer, so both your consumer and your producer are blocked and your program hangs.
Change it to:
void pcdQueue::produce()
{
int i=0;
while(true)
{
{
boost::mutex::scoped_lock lock(qmutex);
while( ! qlen < buffSize ) {
std::cout << "Queue is full" << std::endl;
full.wait(lock);
}
enqueue(i); // or myList.push_back(i) if you switch to std::list
}
empty.notify_one();
}
}
You have the same issues in your consume()
method. Change it to:
pcdFrame* pcdQueue::consume()
{
pcdFrame *frame;
{
boost::mutex::scoped_lock lock(qmutex);
while( qlen == 0 ) {
std::cout << "Queue is empty" << std::endl;
empty.wait(lock);
}
frame = dequeue();
}
full.notify_one();
return frame;
}
In general, take care and note that notifications only have an effect, if someone is waiting. Otherwise, they are "lost". Furthermore, note that you don't need to keep the mutex locked when calling notify_one
(in fact, it can lead to additional context-switch overhead, because you wake up the other thread which will then wait on a mutex that is currently (still) locked (by you). So first, release the mutex, then tell the other thread to continue.
Please note, that both threads run infinitely, so your main program will still hang at the first join()
and never exit. You could include a stopping flag that you checkn in your while
-loops to tell your threads to finish.