Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7809835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:33:38+00:00 2026-06-02T03:33:38+00:00

I figured it out. Silly mistake on my part, I was not actually deleting

  • 0

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.boost.org/doc/libs/1_49_0/libs/circular_buffer/doc/circular_buffer.html#classboost_1_1circular__buffer_19ba12c0142a21a7d960877c22fa3ea00

http://www.drdobbs.com/article/print?articleId=184401518&siteSectionName=cpp

Thread safe implementation of circular buffer

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T03:33:39+00:00Added an answer on June 2, 2026 at 3:33 am

    First of all, instead of writing your own list, you could just wrap std::list in pcdQueueinstead 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I figured out that CUDA does not work in 64bit mode on my mac
Just a silly question that just cant figure out. Ive been working on iPad
I'm just testing out saving to a file from a servlet, and I want
This may be a very silly mistake but I just can't fix it. I
I have a problem, that is maybe silly, but I just can't figure out
This may seem like a silly question but I can't figure it out. let's
I figured out how to clone my form rows and append an incriminting number
I figured out that a constant literal get's placed in the data segment of
I figured out how to get all the user's images in the camera roll
I figured out that of course . and SPACE aren't allowed. Are there other

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.