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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:28:17+00:00 2026-05-21T07:28:17+00:00

I have a class called ‘subscribedQueue’. This class receives its data via its subscribed

  • 0

I have a class called ‘subscribedQueue’. This class receives its data via its subscribed publishers (plural) calling its push method.

In another thread, this class’s pop method is called to receive that data.
Thus in a sense this class is a kind of buffer between multiple publishers and their subscribers. For the implementation I have based myself on information found about thread-safe queues here.

Now my question is twofold:

  • If I would use the same mutex for pusing and popping values (currently I’m using two differrent mutexes), is it possible for my program to get stuck, waiting on a blocked push?
  • If not, how is it possible that both the push and pop methods can get past ‘lock(the_same_mutex)’.

My assumption is that, if I would use the same mutex and the program enters the pop method, it will acquire the lock in pop, check if the queue is empty and wait on the condition variable which can never be set in the push method (as the lock is already acquired by pop).

current Code (using two different mutexes):

#include <boost/thread.hpp>
#include <queue>
#include "subscriber.h"
#include "pubdata.h"
#ifdef DEBUG
#include <iostream>
#include <boost/lexical_cast.hpp>
#endif

namespace PUBLISHSUBSCRIBE
{
  template<class T>
  class SubscribedQueue: public PUBLISHSUBSCRIBE::Subscriber<T>, private std::queue< PubData<T> >
  {
  public:
    PubData<T>  pop();   //removes the next item from the queue, blocks until the queue is not empty
    void push(const PubData<T> data); //method used by the publisher to push data onto the queue
  private:
    mutable boost::mutex writeMutex_; //only needed for publishing/pushing data
    mutable boost::mutex readMutex_;  //only needed for reading/popping data
    boost::condition_variable notify_;
  };

  template<class T>
  PubData<T> SubscribedQueue<T>::pop() { //Blocks until the queue is not empty
    boost::mutex::scoped_lock lock(readMutex_);
    while(std::queue< PubData<T> >::empty())
      notify_.wait(lock); //block until recieving a notification AND the queue is not empty
    PubData<T> head = std::queue< PubData<T> >::front();
    std::queue< PubData<T> >::pop();
#ifdef DEBUG
    std::string debugOut("pop: " + boost::lexical_cast<std::string>(head) + " - timestamp: " + boost::lexical_cast<std::string>(head.timestamp()) + " - from: " + boost::lexical_cast<std::string>(this) + "\n" );
    std::cout <<debugOut;
#endif
    lock.unlock();
    return head;
  }

  template<class T>
  void SubscribedQueue<T>::push(const PubData<T> data){
    boost::mutex::scoped_lock lock(writeMutex_);
#ifdef DEBUG
    std::cout << "published: " << data << std::endl;
#endif
    std::queue< PubData<T> >::push(data);
    lock.unlock();
    notify_.notify_one();
  }
}
#endif //SUBSCRIBEDQUEUE_H

[edit] what worries me most is this: I have a boost::condition_variable notify_ on which a ‘wait until notified’ is performed in pop.
But pop must first lock the mutex, the same mutex that must also be locked in ‘push’ in order to ‘notify’ the condition variable.

So won’t cause this a deadlock, and why not?

  • 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-21T07:28:17+00:00Added an answer on May 21, 2026 at 7:28 am

    Standard library containers aren’t thread safe; If you attempt to modify a container from multiple threads simultaneously then bad things happen.

    If you have a separate mutex for push and pop operations then you don’t protect against a simultaneous push and pop from two threads, so you haven’t really protected the collection at all.

    My assumption is that, if I would use the same mutex and the program enters the pop method, it will acquire the lock in pop, check if the queue is empty and wait on the condition variable which can never be set in the push method (as the lock is already acquired by pop).

    When you wait on the condition variable in pop, wait() unlocks the mutex, so a push() while waiting will be able to lock it. push() calls notify_one() and unlocks the mutex by virtue of the scoped_lock going out of scope at the end of the function. Then, when the pop() thread is next scheduled, it will immediately re-lock the mutex and proceed.

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

Sidebar

Related Questions

I have a class called CategoryViewController and its viewDidLoad method calls this method: -
I have a class called Flamethrower which naturally has its own ammunition that is
i have this class called MemoryManager, it is supposed to implement a simple smart
I have a class called Object which stores some data. I would like to
I have a class called XMLtoXML.java and this is one of it's methods... import
I have a class called RemoteError with a self.fatal method on it. This methods
I have a class called Employee with its 2 private variables and also contains
I have a class called LayoutManager. The purpose of this class is to hold
I have a class called GameState in its own file and that class has
I have this class called Table: class Table { public string Name { get

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.