1

Here is the documentation for std::atomic: http://en.cppreference.com/w/cpp/atomic/atomic

Reading other questions what I've gathered is it generates some sort of special machine instructions (i.e. lock add) and that as a result you don't have to use a mutex around the variable.

What I want to know is how it works and what are the limitations to using them?

3
  • 1
    Any answer you get is going to be really long. A good start is to study up on what lock-free means. From there you can start to read the C++ standard starting with 29 Atomic operations library [atomics], and there's a lot to digest. Commented Jan 11, 2017 at 18:39
  • it does more than generate special machine instructions. It also prevents load/store reordering. The link to the videos in the answer below really should be followed - you'll be amazed at what you learn. You'll need 2 hours. Commented Jan 11, 2017 at 21:27
  • Thank you. I will watch the videos. I didn't realize it was so complicated. Commented Jan 12, 2017 at 14:35

1 Answer 1

1

This topic really could fill a book. Likely you won't get any clear answers in this format.

Check out Herb Sutters videos, these are really the clearest discussions I've seen on the topic:

https://herbsutter.com/2013/02/11/atomic-weapons-the-c-memory-model-and-modern-hardware/

See the links for "Part 1" and "Part 2".

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.