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