10

To be precise, I only need to increase a double by another double and want it to be thread safe. I don't want to use mutex for that since the execution speed would dramatically decrease.

1

2 Answers 2

18

As a rule, the C++ standard library tries to provide only operations that can be implemented efficiently. For std::atomic, that means operations that can be performed lock-free in an instruction or two on "common" architectures. "Common" architectures have atomic fetch-and-add instructions for integers, but not for floating point types.

If you want to implement math operations for atomic floating point types, you'll have to do so yourself with a CAS (compare and swap) loop (Live at Coliru):

std::atomic<double> foo{0};

void add_to_foo(double bar) {
  auto current = foo.load();
  while (!foo.compare_exchange_weak(current, current + bar))
    ;
}
4
  • Thank You for the answer, I didn't realize that in order to do that (add two doubles thread safely) I would need such dirty (or at least it looks like it to me) workaround.
    – Noozen
    Apr 16, 2014 at 18:12
  • 1
    I am a little confused. Why will this code not go into an infinite loop if foo != current due to foo being modified by another thread between the load and the CAS?
    – Joe
    Nov 17, 2015 at 1:55
  • 2
    @Joe compare_exchange_weak takes its first argument by reference, and updates it to the observed value on failure. So if the CAS fails becayse foo != current the loop calculates a new current + bar from the updated current and tries again.
    – Casey
    Nov 17, 2015 at 3:26
  • @Casey Thanks for the clarification.
    – Joe
    Nov 18, 2015 at 18:45
-1

So use the integral atomic as a memory barrier. Here's a page with source and explanation: http://preshing.com/20121019/this-is-why-they-call-it-a-weakly-ordered-cpu/

1
  • A memory barrier only helps with ordering, not atomicity. This is why you need a CAS (or LL/SC) to make the entire read-modify-write atomic. Sep 4, 2017 at 5:04

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.