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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:34:57+00:00 2026-06-16T07:34:57+00:00

Suppose that I have a class that looks like this (actually exactly this size):

  • 0

Suppose that I have a class that looks like this (actually exactly this size):

class K
{
public:

    long long get_x() const; // lock m_mutex in shared/read-only mode

    void update( long long w ); // lock m_mutex with a unique_lock

private:
    long long m_a;
    long long m_b;
    long long m_c;
    long long m_x;
    double m_flow_factor;

    mutable boost::shared_mutex m_mutex;
};

As you can see, this should be thread-safe. The update function is called by one thread at a time, unknown but only one thread (guaranteed), but the accessor can be called by several threads at the same time.

The update function is changing all the values and is called very often (a hundread times a second). Current implementation will, as you can guess, locks a lot.

I was considering using std::atomic to avoid locks and potentially make this code more efficient. However, I really need the update function to update the values together.
Therefore, I am considering doing something like this instead:

class K
{
public:

    long long get_x() const
    { return data.load().x; }

    void update( long long w )
    {
        auto data_now = data.load();
        // ... work with data_now
        data.store( data_now );
    }

private:
    struct Data {
    long long a;
    long long b;
    long long c;
    long long x;
    double flow_factor;
    };
    std::atomic<Data> data;
};

My current understanding of std::atomic is that, even if this code is more readable than the previous one (because it don’t have lock declarations everywhere), as the K::Data struct is “big”, std::atomic will just be implemented with a normal mutex lock (so it shouldn’t be faster than my initial implementation anyway).

Am I correct?

  • 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-16T07:34:58+00:00Added an answer on June 16, 2026 at 7:34 am

    Any specialization for std:atomic for a struct like that is going to involve internal locking, so you’ve gained nothing, and now you also have a data race between the load and store you didn’t have before, as this had exclusive locking around the whole block (i presume?) in the previous version.

    Also with the shared_mutex, it might be wise to profile with a normal mutex vs shared_mutex, you may find the normal mutex performs better (all depends on how long you’re holding your locks for).

    The benefit of the shared_mutex is only seen when locks are being held for reading for an extended period of time and there are very few writes, otherwise the overhead involved in the shared_mutex kills any gains you would have over the normal mutex.

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

Sidebar

Related Questions

Suppose I have an entity in Fluent NHibernate that looks like this: public class
suppose i have a jax-rs resource class that looks like this: @Path(/nodes) public class
Suppose I have a class that looks like this: class Derived : // some
Suppose I have an autolocker class which looks something like this: template <T> class
Suppose I have a domain model like this: public class MyDomainEntity { public double
I have some HTML that looks like this: <h2>Heading</h2> <div class='myClass'> <h2>Another Heading</h2> </div>
I have a class that looks like: class A { public: A(); void MethodThatWillNotBeInOtherStructure();
Suppose I have a class like this: class Foo { std::vector<int> bar; public: std::vector<int>&
Suppose I have an abstract class FactorizedDialog . It looks like this (please note
First suppose that I have an abstract class, let's call it AbstractClass. Suppose public

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.