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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:30:03+00:00 2026-06-10T17:30:03+00:00

I was wondering how to share a mutex in one class amongst different instances

  • 0

I was wondering how to share a mutex in one class amongst different instances of another class.

Right now, I have a class, Indexer, that has a Boost mutex and condition_variable as private member variables. I create an auto_ptr of the Indexer class in my main and pass a pointer of Indexer to instances of another class, Robot.

I do that like the following:

std::auto_ptr<Indexer> index_service(new Indexer());
Robot a(*index_service.get());
Robot b(*index_service.get());
Robot c(*index_service.get());

Robot’s constructor is:

Robot(Indexer &index_service)
{
  this->index_service = index_service;
}

Robot’s header looks like:

class Robot
{
   public:
     Robot(Indexer &index_service);
   private:
     Indexer index_service;
};

However, since mutexes are noncopyable, I get an error.

I was thinking of making the mutex and condition_variable shared_ptrs but I read that this could result in unexpected behaviour.

Can someone please show me the proper/correct way to do this?

Thank you!

  • 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-10T17:30:04+00:00Added an answer on June 10, 2026 at 5:30 pm

    I’m not a C++ guru, but it seems like the unholy mix of pass by reference and pointers is trouble here.

    Specifically you do this: this->index_service = index_service;
    But the value is passed in as Indexer &index_service
    And since the local index_service is type Indexer index_service;
    I believe that the assignment implies copy.

    I assume you only want one instance of the Indexer, so you are really meaning to store a reference to it in your Robot class. To do that you would want to have your constructer take a pointer (which is what the *index_service.get() call is getting you anyway). Further you want your class variable to be a pointer type.

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

Sidebar

Related Questions

I was wondering, if we have two tables that share one column in common
Using Meteor, I'm wondering how best to handle different client-side collections that share the
I have two documents that should share a numeric value that should also increase
I have an interesting challenge that I'm wondering if anyone here can give me
Just wondering what tips or tricks you guys might have to share. As always
Just wondering if there is any risk that I could prematurely share a website
I was wondering if there are some PSP programmers here that could share their
I'm just wondering if anyone knows of a class that exists for handling user
I have multiple content types, but they all share some similarities. I'm wondering when
I was wondering if people would share their best practices / strategies on handling

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.