Lazer
Lazer

Reputation: 94800

Does C++11 std::atomic guarantee mutual exclusion as well as sequential consistency?

I believe the answer is yes, much like in Java.

Please correct me if I am wrong.

If I need to just use mutual exclusion, I can use std::mutex and others.

What if I need just sequential consistency and not mutual exclusion? What can be used for that?

Upvotes: 1

Views: 260

Answers (2)

Wandering Logic
Wandering Logic

Reputation: 3403

The individual operations performed on objects of type std::atomic<whatever> are atomic, but that's as far as it goes. So std::atomic<int>::fetch_add() is atomic. But

std::atomic<int> x;
...
int tmp = x.load();
tmp += 1;
x.store(tmp);

Is just sequentially consistent, not atomic.

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 490028

Yes -- see std::atomic with memory_order_seq_cst for sequential consistency.

Upvotes: 1

Related Questions