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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:35:38+00:00 2026-05-30T12:35:38+00:00

I need to protect the access to a data structure in my class. As

  • 0

I need to protect the access to a data structure in my class. As I can’t have mutex (because I can’t copy it) I am considering to have shared_ptr and keep the mutex there. Here is a sample code of my idea:

class Sample {
    typedef boost::lock_guard<boost::mutex> AcquireLock;
    boost::shared_ptr<boost::mutex> mutt;

public:
    Sample() : mutt(new boost::mutex) {}

    void Method()
    {
        AcquireLock lock(*mutt);

        //do some work here
    }
};

I’ve got the following questions:

  • Is it a bad practice to use the mutex that way (as member of the class, via shared_ptr)?
  • Should I have copy constructor for this class, as it has memory allocated on heap via shared_ptr?

EDIT: Maybe I need to give a bit more details:
I’ll create this object only once and save it in std::vector. I don’t need to make copies of it and if the vector needs to make copies, I don’t want to have different mutex for each copy. That’s why I think the copy constructor will work for me.

  • 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-30T12:35:39+00:00Added an answer on May 30, 2026 at 12:35 pm

    If you make a copy of a Sample object, the copy constructor will be called, either one generated automatically by the compiler, or one that you have written explicitly.

    Whether it’s a good idea to allow copies of Sample objects depends on what you are trying to do.
    If it doesn’t make sense to allow copies, then make the object non-copyable, e.g. by giving a private prototype for the copy constructor.

    If you do want to allow copies, then you need to decide if each copy should have its own mutex, and define the copy constuctor appropriately. The automatically generated copy constructor will only do a shallow copy, so all copies would share the mutex.

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

Sidebar

Related Questions

I have a class that has some data members that I want to be
i have a project in c++03 that have a problem with data structure: i
In my asp.net project currently i have business logic and and data access code
best solution for use access in project for database with security i need to
I am generating a reports using SQL Reporting services 2005. I need to protect
The Junits I have in my project need to load property files from the
to my project i need document editor for many types of documents(tabular data, invoices,
I have been using the encryption class of codeigniter (PHP framework) for a while
Following on from this question, I now have the following structure: Wolfie.Core - Contains
I want to use Entity Framework for my data access on a project I'm

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.