Questions tagged [stdatomic]

std::atomic is a class template in the C++11 Standard Library which provides atomic operations.

std::atomic is a class template in the C++11 Standard Library, defined in the <atomic> header. An atomic object of type std::atomic<X> provides member functions to perform atomic operations on its member of type X.

Like mutexes, atomic objects can be used for synchronization in multi-threaded C++ programs. Unlike mutexes or other locks, atomics can be used to build algorithms that allow concurrent readers and writers.

Atomics can even be used to build custom locking primitives (but that's usually a bad idea on systems with OS-supported locking functions that yield the CPU on contention).

Resources:

391 questions
99
votes
6 answers

When do I really need to use atomic instead of bool?

Isn't atomic redundant because bool is atomic by nature? I don't think it's possible to have a partially modified bool value. When do I really need to use atomic instead of bool?
user955249
76
votes
2 answers

What does the [[carries_dependency]] attribute mean?

Can someone explain it in a language that mere mortals understand?
Yakov Galka
  • 61,035
  • 13
  • 128
  • 192
74
votes
5 answers

c++, std::atomic, what is std::memory_order and how to use them?

Can anyone explain what is std::memory_order in plain English, and how to use them with std::atomic<>? I found the reference and few examples here, but don't understand at all. http://en.cppreference.com/w/cpp/atomic/memory_order
2607
  • 3,557
  • 12
  • 45
  • 62
74
votes
3 answers

Where is the lock for a std::atomic?

If a data structure has multiple elements in it, the atomic version of it cannot (always) be lock-free. I was told that this is true for larger types because the CPU can not atomically change the data without using some sort of lock. for…
curiousguy12
  • 1,241
  • 6
  • 12
53
votes
9 answers

Why don't compilers merge redundant std::atomic writes?

I'm wondering why no compilers are prepared to merge consecutive writes of the same value to a single atomic variable, e.g.: #include std::atomic y(0); void f() { auto order = std::memory_order_relaxed; y.store(1, order); …
PeteC
  • 937
  • 8
  • 15
50
votes
2 answers

Does std::atomic work appropriately?

I am reading through Anthony Williams' "C++ Concurrency in Action" and in Chapter 5, which talks about the new multithreading-aware memory model and atomic operations, and he states: In order to use std::atomic for some user-defined UDT, this…
Thomas Russell
  • 5,480
  • 3
  • 28
  • 61
46
votes
4 answers

How do memory_order_seq_cst and memory_order_acq_rel differ?

Stores are release operations and loads are acquire operations for both. I know that memory_order_seq_cst is meant to impose an additional total ordering for all operations, but I'm failing to build an example where it isn't the case if all the…
AProgrammer
  • 48,232
  • 8
  • 83
  • 139
35
votes
2 answers

"Use of deleted function" error with std::atomic_int

I want to use an std::atomic_int variable. In my code, I have: #include std::atomic_int stop = 0; int main() { // Do something } And this gives me a compile error: use of deleted function…
Karnivaurus
  • 18,315
  • 44
  • 129
  • 209
30
votes
1 answer

C++11 memory_order_acquire and memory_order_release semantics?

http://en.cppreference.com/w/cpp/atomic/memory_order, and other C++11 online references, define memory_order_acquire and memory_order_release as: Acquire operation: no reads in the current thread can be reordered before this load. Release…
Cedomir Segulja
  • 357
  • 1
  • 3
  • 6
25
votes
2 answers

Why isn't atomic double fully implemented

My question is quite simple. Why isn't std::atomic implemented completely? I know it has to do with atomic RMW (read-modify-write) access. But I really don't see, why this shouldn't be possible on a double. It's specified that any trivially…
laurisvr
  • 2,636
  • 2
  • 19
  • 41
24
votes
3 answers

Acquire/release semantics with 4 threads

I am currently reading C++ Concurrency in Action by Anthony Williams. One of his listing shows this code, and he states that the assertion that z != 0 can fire. #include #include #include std::atomic
Aryan
  • 558
  • 4
  • 15
20
votes
6 answers

What exact rules in the C++ memory model prevent reordering before acquire operations?

I have a question regarding the order of operations in the following code: std::atomic x; std::atomic y; int r1; int r2; void thread1() { y.exchange(1, std::memory_order_acq_rel); r1 = x.load(std::memory_order_relaxed); } void…
20
votes
2 answers

Can atomic loads be merged in the C++ memory model?

Consider the C++ 11 snippet below. For GCC and clang this compiles to two (sequentially consistent) loads of foo. (Editor's note: compilers do not optimize atomics, see this Q&A for more details, especially http://wg21.link/n4455 standards…
18
votes
1 answer

Why does std::atomic constructor behave different in C++14 and C++17

I'm working in a project with C++11 and I tried following code #include struct A { std::atomic_int idx = 1; }; int main() { return 0; } I get the compiler error error: use of deleted function…
Thomas Sablik
  • 15,040
  • 7
  • 26
  • 51
18
votes
1 answer

Acquire/release semantics with non-temporal stores on x64

I have something like: if (f = acquire_load() == ) { ... use Foo } and: auto f = new Foo(); release_store(f) You could easily imagine an implementation of acquire_load and release_store that uses atomic with load(memory_order_acquire) and…
Eloff
  • 18,456
  • 14
  • 72
  • 96
1
2 3
26 27