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:

591 questions
132
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
90
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
  • 4,037
  • 13
  • 49
  • 64
85
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,741
  • 1
  • 10
  • 15
85
votes
2 answers

Must I call atomic load/store explicitly?

C++11 introduced the std::atomic<> template library. The standard specifies the store() and load() operations to atomically set / get a variable shared by more than one thread. My question is are assignment and access operations also atomic?…
bavaza
  • 10,319
  • 10
  • 64
  • 103
81
votes
2 answers

What does the [[carries_dependency]] attribute mean?

Can someone explain it in a language that mere mortals understand?
Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
64
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
  • 1,047
  • 9
  • 15
61
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,870
  • 4
  • 33
  • 68
59
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
  • 51,233
  • 8
  • 91
  • 143
45
votes
7 answers

Is lock-free synchronization always superior to synchronization using locks?

In C++, there is one atomic type std::atomic. This atomic type may be lock-free or maybe not depending on the type T and on the current platform. If a lock-free implementation for a type is available in a platform for a type T, then most…
Sourav Kannantha B
  • 2,860
  • 1
  • 11
  • 35
45
votes
2 answers

Acquire/Release versus Sequentially Consistent memory order

For any std::atomic where T is a primitive type: If I use std::memory_order_acq_rel for fetch_xxx operations, and std::memory_order_acquire for load operation and std::memory_order_release for store operation blindly (I mean just like resetting…
zahir
  • 1,298
  • 2
  • 15
  • 35
44
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
  • 22,823
  • 57
  • 147
  • 247
41
votes
3 answers

Do I have to use atomic for "exit" bool variable?

I need to set a flag for another thread to exit. That other thread checks the exit flag from time to time. Do I have to use atomic for the flag or just a plain bool is enough and why (with an example of what exactly may go wrong if I use plain…
PowerGamer
  • 2,106
  • 2
  • 17
  • 33
35
votes
3 answers

How to initialize a static std::atomic data member

I would like to generate identifiers for a class named order in a thread-safe manner. The code below does not compile. I know that the atomic types do not have copy constructors, and I assume that explains why this code does not work. Does anybody…
Teisman
  • 1,318
  • 2
  • 13
  • 26
35
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
  • 417
  • 1
  • 4
  • 6
32
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,724
  • 6
  • 25
  • 44
1
2 3
39 40