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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:36:06+00:00 2026-05-29T22:36:06+00:00

I’m trying to understand how to use mutexes with objects in c++. I have

  • 0

I’m trying to understand how to use mutexes with objects in c++. I have the following (trivial) multi-threaded code I’m using as a speed test:

 struct Rope{
   int n, steps, offset;
   //std::mutex mut;

   Rope() {}
   Rope(int n, int steps, int offset) : n(n), steps(steps), offset(offset) {}

   void compute(){
     double a[n];
     for (int i=0; i<n; i++)
       a[i] = i + offset;
     for (int step=0; step<steps; step++)
       for (int i=0; i<n; i++)
     a[i] = sin(a[i]);
   }

 };

 void runTest(){
   int numRuns = 30;
   int n = 10000;
   int steps = 10000;

   std::vector<Rope> ropes;
   std::vector<std::thread> threads;
   for (int i=0; i<numRuns; i++)
     ropes.push_back(Rope(n, steps, i));
   for (auto& r : ropes)
     threads.push_back(std::thread(&Rope::compute, r));
   for (std::thread& t : threads)
     t.join();
 }    

The code runs fine as is, and sees a ~4x speedup on my 4 core machine. I am, of course, not storing anything in the Rope, so there is no need for a mutex. If I now presume that I did have some data I needed to protect, I’d like to attach a mutex to the Rope and (e.g.) call std::lock_guard within the compute() loop. If I uncomment the mutex, though, I get a bunch of compiler errors about “use of deleted function” for assignment and copy operators. What am I missing in my goal to safely lock an object?

  • 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-29T22:36:07+00:00Added an answer on May 29, 2026 at 10:36 pm

    The straightforward way to make a class threadsafe is to add a mutex attribute and lock the mutex in the accessor methods

    class cMyClass {
      boost::mutex myMutex;
      cSomeClass A;
    public:
      cSomeClass getA() {
        boost::mutex::scoped_lock lock( myMutex );
        return A;
      }
    };
    

    The problem is that this makes the class non-copyable. This is important, especially if you want to store objects of the class in a container.

    I can make things work by making the mutex a static.

    class cMyClass {
      static boost::mutex myMutex;
      cSomeClass A;
    public:
      cSomeClass getA() {
        boost::mutex::scoped_lock lock( myMutex );
        return A;
      }
    };
    

    However, this means that every instance of the class blocks when any other instance is being accessed, because they all share the same mutex.

    Theoretically, a class containing a non-static mutex can be made copyable by hand coding the copy constructor and assignment operator so as to leave out the mutex. However, this is difficult and tedious to do properly, especially for a class with a large number of attributes that frequently change during development.

    If a static mutex that blocks access to all instances of the class when one is blocked is not acceptable, then the best and simplest approach is to maintain the mutexes outside the class. It might seem unfortunate to expose the inner workings of a class in this way, but the alternatives are way more complex, hence unreliable, and I frequently find significant optimizations when the mutexes are handled at the level of the code accessing the class.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I have this code to decode numeric html entities to the UTF8 equivalent character.
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Basically, what I'm trying to create is a page of div tags, each has

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.