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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:12:39+00:00 2026-05-13T07:12:39+00:00

I have the following problem. I have two classes, in this case A and

  • 0

I have the following problem.

I have two classes, in this case A and B, which both own a concurrent_queue. The assumption here is that concurrent_queue is a thread-safe queue, with a blocking push() function. When an object is enqueued in B, it accesses the singleton A and it is queued up in A as well. This has the effect that a whole bunch of B’s have little queues with their own objects, and one large queue in A that contains them all. Each instance of B could live in a separate thread.

What I am encountering is that frequently a thread will get pre-empted between the two lines of code in B::foo(), meaning A::mQueue contains the object, but B::mQueue does not yet contain the object.

What I am wondering is how I can ensure that when B::foo() is called that the object is either pushed onto both queues or neither queue. It seems to me that I would have to have a mutex in A that B can get a hold of, and lock A’s mutex in B::foo().

Does anyone have any suggestions how I could accomplish this, or how I could restructure my code to accomplish this? I am using the boost::threading library.

Class A
{    
public:
    A& instance(){/* return singleton */}        
    void addToQueue(SomeObject const& obj)
    {
        mQueue.push(obj);
    }        
private:
    concurrent_queue<SomeObject> mQueue;
};

Class B
{
public:
    void foo()
    {
        SomeObject obj;
        //I would like to guarantee that obj is either present in both queues or neither queue
        A::instance().addToQueue(obj);
        mQueue.push(obj);
    }        
private:
    concurrent_queue<SomeObject> mQueue;
};

In my actual implementation, it is not the same object that gets queued up in A and B, rather the A queues up structs that contain pointers to B’s, which let me dequeue everything in A and dequeue from all the B’s in the same order that they were queued up in, but this should be irrelevant to the question.

  • 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-13T07:12:39+00:00Added an answer on May 13, 2026 at 7:12 am

    You’ll need to atomicize your operation of “adding objects to both queues.” You’ll need a lock or some other kind of synchronization primitive around your two function calls. Same for removing items from the queues.

    boost::mutex looks fit for the job. You’ll need a single instance and need it to be accessible from anywhere the queues are modified. Since it will also have the same lifetime as A’s queue, I suggest you put it in A. Then modify queue accesses so they look like:

    A::instance().lockQueue(); //calls A.mQueueAccessMutex.lock(), probably
        A::instance().addToQueue(obj);
        mQueue.push(obj);
    A::instance().unlockQueue();
    

    Or, RAII-style:

    {
        LockHolder lh(A::instance().getLock()); //lock called in lh's constructor
    
        A::instance().addToQueue(obj);
        mQueue.push(obj);
    
        //unlock called in lh's destructor
    }
    

    Note that concurrent_queue will then be redundant since no two threads will be accessing the queue concurrently.

    —

    And, of course, there’s always the very small chance that simply reversing the order you put the items in the queues will solve your problems. :)

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

Sidebar

Related Questions

Avast there fellow programmers! I have the following problem: I have two rectangles overlapping
I have the following problem. If I query values with a keyfigure which is
I have the following problem: I have an HTML textbox ( <input type=text> )
I have the following problem using subversion: I'm currently working on the trunk of
I have the following problem using template instantiation [*]. file foo.h class Foo {
I have the following problem in my Data Structures and Problem Solving using Java
I have the following problem: I open the dialog, open the SIP keyboard to
I have got the following problem since the server has safe mode turned on,
Has anyone encountered the following problem: I have IIS7 running on my computer. On
When writing GUIs, I've frequently come over the following problem: Assume you have a

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.