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

  • Home
  • SEARCH
  • 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 9024589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:03:20+00:00 2026-06-16T06:03:20+00:00

The definitions for std::atomic<> seem to show its obvious usefulness for primitive or perhaps

  • 0

The definitions for std::atomic<> seem to show its obvious usefulness for primitive or perhaps POD-types.

When would you actually use it for classes?

When should you avoid using it for classes?

  • 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-16T06:03:21+00:00Added an answer on June 16, 2026 at 6:03 am

    The operations std::atomic makes available on any trivially copyable type are pretty basic. You can construct and destroy atomic<T>, you can ask if the type is_lock_free(), you can load and store copies of T, and you can exchange values of T in various ways. If that’s sufficient for your purpose then you might be better off doing that than holding an explicit lock.

    If those operations aren’t sufficient, if for example you need to atomically perform a sequence operations directly on the value, or if the object is large enough that copying is expensive, then instead you would probably want to hold an explicit lock which you manage to achieve your more complex goals or avoid doing all the copies that using atomic<T> would involve.

    // non-POD type that maintains an invariant a==b without any care for
    // thread safety.
    struct T { int b; }
    struct S : private T {
        S(int n) : a{n}, b{n} {}
        void increment() { a++; b++; }
    private:
        int a;
    };
    
    std::atomic<S> a{{5}}; // global variable
    
    // how a thread might update the global variable without losing any
    // other thread's updates.
    S s = a.load();
    S new_s;
    do {
        new_s = s;
        new_s.increment(); // whatever modifications you want
    } while (!a.compare_exchange_strong(s, new_s));
    

    As you can see, this basically gets a copy of the value, modifies the copy, then tries to copy the modified value back, repeating as necessary. The modifications you make to the copy can be as complex as you like, not simply limited to single member functions.

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

Sidebar

Related Questions

It is widely known that adding declarations/definitions to namespace std results in undefined behavior.
I did recently start to use records for my port definitions, especially if I
i would like to use only certain functions from math.h (WITHOUT including the entire
Why definition of std::function<>::operator() in the C++ standard is: R operator()(ArgTypes...) const; and not
I know that definitions for :hover must come before the definition for :active in
A few definitions first: Definition 1 A graph G = (V, E) is called
I have the following definitions: public class BaseEntity { ... public BaseEntity() { }
File1: hello - dictionary definitions: hi hello hallo greetings salutations no more hello for
I've found many definitions of the 'var' statement but most of them are incomplete
Vim is indenting method definitions an extra level after an attribute. This is driving

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.