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
31
votes
3 answers

Memory fences: acquire/load and release/store

My understanding of std::memory_order_acquire and std::memory_order_release is as follows: Acquire means that no memory accesses which appear after the acquire fence can be reordered to before the fence. Release means that no memory accesses which…
Siler
  • 8,976
  • 11
  • 64
  • 124
29
votes
4 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
  • 608
  • 6
  • 15
26
votes
3 answers

How to use std::atomic efficiently

std::atomic is new feature introduced by c++11 but I can't find much tutorial on how to use it correctly. So are the following practice common and efficient? One practice I used is we have a buffer and I want to CAS on some bytes, so what I did…
Kan Li
  • 8,557
  • 8
  • 53
  • 93
26
votes
2 answers

C++11: the difference between memory_order_relaxed and memory_order_consume

I am now learning C++11 memory order model and would like to understand the difference between memory_order_relaxed and memory_order_consume. To be specific, I am looking for a simple example where one cannot replace memory_order_consume with…
TruLa
  • 1,031
  • 11
  • 21
23
votes
0 answers

`is_always_lock_free` gives `true` but `is_lock_free()` gives `false` on macOS, why?

I'm experimenting with C++ atomic's std::atomic::is_always_lock_free and std::atomic::is_lock_free. I wrote a simple struct A and want to know if the atomic version of A is lock-free: #include #include using namespace…
Shuai
  • 962
  • 1
  • 10
  • 21
22
votes
1 answer

Is assignment equivalent to load/store for std::atomic

I see that this is potentially answered in question Must I call atomic load/store explicitly?. So for sake of clarity I will restate my question succinctly in the hopes that future readers find this clear. Is std::atomic b(false); bool x =…
BGR
  • 669
  • 5
  • 10
22
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…
22
votes
2 answers

c++11 interprocess atomics and mutexes

I have a Linux program which spawns several processes (fork) and communicates through POSIX Shared Memory. I'd like to have each process allocate an id (0-255). My intention is to place a bitvector in the shared memory region (initialized to zero)…
dschatz
  • 1,188
  • 2
  • 13
  • 25
21
votes
1 answer

Why is copy assignment of volatile std::atomics allowed?

std::atomic has deleted copy assignment operators. Hence, the following results in a compiler error: std::atomic a1, a2; a1 = a2; // Error I think the motivation for the deleted operators is explained e.g. in this post. So far, so good. But I…
Sedenion
  • 5,421
  • 2
  • 14
  • 42
21
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…
21
votes
3 answers

No type named 'atomic' in namespace 'std'

Why doesn't std::atomic index; Work? Currently using LLVM 3.1 with these params C Language Dialect GNU [-std=gnu99] C++ Language Dialect [-std=c++11] C++ Standard Library libc++(LLVM C++ standard library with C++11 support)
Hobbyist
  • 885
  • 2
  • 10
  • 23
20
votes
2 answers

Difference between std::atomic and std::condition_variable wait, notify_* methods

I was looking through 'Atomic operations library' and came across a new c++20 feature of atomic 'wait' and 'notify_' methods. I am curious on what the differences are in regards to std::condition_variable's 'wait' and 'notify_' methods.
PVRT
  • 480
  • 4
  • 13
20
votes
3 answers

Atomic shared_ptr for lock-free singly linked list

I'm wondering if it is possible to create a lock-free, thread-safe shared pointer for any of the "common" architectures, like x64 or ARMv7 / ARMv8. In a talk about lock-free programming at cppcon2014, Herb Sutter presented a (partial) implementation…
MikeMB
  • 20,029
  • 9
  • 57
  • 102
19
votes
2 answers

std::atomic::notify_one could unblock multiple threads

According to cppreference, std::atomic::notify_one() will notify at least one thread that is waiting on said atomic. This means that according to the standard it could unblock more than one thread. This is in contrast to…
L0laapk3
  • 890
  • 10
  • 26
19
votes
1 answer

Why does the thread sanitizer complain about acquire/release thread fences?

I'm learning about different memory orders. I have this code, which works and passes GCC's and Clang's thread sanitizers: #include #include #include int state = 0; std::atomic_int a = 0; void foo(int from, int to)…
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
1
2
3
39 40