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 3287708
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:31:26+00:00 2026-05-17T20:31:26+00:00

I have implemented a basic threaded producer-consumer (thread 1 = producer, thread 2 =

  • 0

I have implemented a basic threaded producer-consumer (thread 1 = producer, thread 2 = consumer) using Boost threads and conditions. I am getting stuck in wait() indefinitely quite often. I can’t really see what could be wrong here. Below is some pseudo-code:

// main class
class Main {
public:
  void AddToQueue(...someData...)
  {
    boost::mutex::scoped_lock lock(m_mutex);
    m_queue.push_back(new QueueItem(...someData...));
    m_cond.notify_one(); 
  }

  void RemoveQueuedItem(...someCond...)
  {
    // i'm wondering if this could cause the trouble?
    boost::mutex::scoped_lock lock(m_mutex);
    // erase a item matching condition (some code not shown,
    // but should be fairly self-explanatory -- IsMatch()
    // simply looks at a flag of QueueItem
    m_queue.erase(std::remove_if(m_queue.being(), m_queue.end(),
      boost::bind(&Main::IsMatch, this, _1, someCond), m_queue.end());
  }

  friend void WorkerThread(Main* m);
private:      
  boost::ptr_deque<QueueItem> m_queue;
  boost::mutex m_mutex;
  boost::condition m_cond;
};

// worker thread
void WorkerThread(Main* m)
{
  typedef boost::ptr_deque<QueueItem>::auto_type RelType;
  RelType queueItem;

  while(!shutDown) {
    { // begin mutex scope
      boost::mutex::scoped_lock lock(m->m_mutex);
      while(m->m_queue.empty()) {
        m->m_cond.wait(lock); // <- stuck here forever quite often!
      }
      queueItem = m->m_queue->pop_front(); // pop & take ptr ownership
    } // end mutex scope

    // ... do stuff with queueItem
    // ...
    // ... queueItem is deleted when it leaves scope & we loop around
  }
}

Some additional information:

  • Using Boost v1.44
  • Issue is occurring in Linux and Android; I’m not yet sure if it happens in Windows

Any ideas?

UPDATE:
I believe I have isolated the issue. I’ll update further once confirmed, which hopefully will be tomorrow.

UPDATE 2:
It turns out there is no issue in the code described above. I was reliant on a underlying API for AddToQueue() – when processing data in the worker thread & handing it back to the API, it had a circular bug where it would call AddToQueue() again… which is now fixed 😉

  • 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-05-17T20:31:26+00:00Added an answer on May 17, 2026 at 8:31 pm

    I did something similar recently even though mine uses the STL queue. See if you can pick out from my implementation. As wilx says, you need to wait on the condition. My implementation has maximum limit on the elements in the queue and I use that to wait for the mutex/guard to be freed.

    I originally did this on Windows with ability to use Mutex or Critical sections in mind hence the template parameter which you can remove and use boost::mutex directly if it simplifies it for you.

    #include <queue>
    #include "Message.h"
    #include <boost/thread/locks.hpp>
    #include <boost/thread/condition.hpp>
    
    template <typename T> class Queue :  private boost::noncopyable
    {
    public:
      // constructor binds the condition object to the Q mutex
      Queue(T & mutex, size_t max_size) :  m_max_size(max_size), m_mutex(mutex){}
    
      // writes messages to end of Q 
      void put(const Message & msg)
      {
        // Lock mutex to ensure exclusive access to Q
        boost::unique_lock<T> guard(m_mutex);
    
        // while Q is full, sleep waiting until something is taken off of it
        while (m_queue.size() == m_max_size)
        {
          cond.wait(guard);
        }
    
        // ok, room on the queue. 
        // Add the message to the queue
        m_queue.push(msg);
    
        // Indicate so data can be ready from Q
        cond.notify_one();
      }
    
      // Read message from front of Q. Message is removed from the Q
      Message get(void)
      {
        // Lock mutex to ensure exclusive access to Q
        boost::unique_lock<T> guard(m_mutex);
    
        // If Q is empty, sleep waiting for something to be put onto it
        while (m_queue.empty())
        {
          cond.wait(guard);
        }
    
        // Q not empty anymore, read the value
        Message msg = m_queue.front();
    
        // Remove it from the queue
        m_queue.pop();
    
        // Signal so more data can be added to Q
        cond.notify_one();
    
        return msg;
      }
    
      size_t max_size(void) const
      {
        return m_max_size;
      }
    
    
    private:
      const size_t m_max_size;
      T & m_mutex;
      std::queue<Message> m_queue;
      boost::condition_variable_any cond;
    };
    

    This way, you can share the queue across the producer/consumer. Example usage

    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 Producer/Consumer defined as below

    template <typename T> class Producer
    {
    public:
       // Queue passed in
       explicit Producer(Queue<T> &q) :  m_queue(q) {}
    
       void operator()()
       {
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented a python webserver. Each http request spawns a new thread. I
I have implemented tracing based on System.Diagnostics. I am also using a System.Diagnostics.TextWriterTraceListener, and
We have implemented a popup window as a modal dialog using the IE method:
I am trying to implement performance testing on ActiveMQ, so have setup a basic
I have implemented what I thought was a pretty decent representation of MVC in
I have implemented a simple file upload-download mechanism. When a user clicks a file
I have implemented a SAX parser in Java by extending the default handler. The
I have implemented VirtualPathProvider class so I can keep all my views in the
I have implemented authentication systems for webapps several times over the years, but before
I have implemented a linked list as a self-referencing database table: CREATE TABLE LinkedList(

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.