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
19
votes
2 answers

Difference between memory_order_consume and memory_order_acquire

I have a question regarding a GCC-Wiki article. Under the headline "Overall Summary" the following code example is given: Thread 1: y.store (20); x.store (10); Thread 2: if (x.load() == 10) { assert (y.load() == 20) y.store (10) } It is said…
splotz90
  • 321
  • 2
  • 11
18
votes
1 answer

Atomic function pointer call compiles in gcc, but not in clang and msvc

When calling function from an atomic function pointer, like: #include #include int func0(){ return 0; } using func_type = std::add_pointer::type; std::atomic f = { func0 }; int main(){ f(); } gcc…
ja2142
  • 892
  • 15
  • 23
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
  • 16,127
  • 7
  • 34
  • 62
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
  • 20,828
  • 17
  • 83
  • 112
18
votes
4 answers

Is it necessary to use a std::atomic to signal that a thread has finished execution?

I would like to check if a std::thread has finished execution. Searching stackoverflow I found the following question which addresses this issue. The accepted answer proposes having the worker thread set a variable right before exiting and having…
Robert Rüger
  • 851
  • 9
  • 21
17
votes
2 answers

Compare and swap C++0x

From the C++0x proposal on C++ Atomic Types and Operations: 29.1 Order and Consistency [atomics.order] Add a new sub-clause with the following paragraphs. The enumeration memory_order specifies the detailed regular (non-atomic) memory…
axel22
  • 32,045
  • 9
  • 125
  • 137
17
votes
1 answer

What is the difference between explicit atomic load/store and usual operator= and operator T?

Consider these two variants: std::atomic a; a = 1; int b = a; and std::atomic a; a.store(1); int b = a.load(); I see from the documentation that the second one is fully atomic, but I don't understand when I should use which and what's…
Juster
  • 732
  • 1
  • 10
  • 26
17
votes
1 answer

How do I elegantly zero-initialize an array of std::atomic?

Let's say I have a class with a member array of std::atomics, where the array is sized via a computation (i.e. it may change based on other constants elsewhere in the program): class Foo { static constexpr size_t kArraySize = ComputeArraySize(); …
jacobsa
  • 5,719
  • 1
  • 28
  • 60
16
votes
6 answers

C++ standard: can relaxed atomic stores be lifted above a mutex lock?

Is there any wording in the standard that guarantees that relaxed stores to atomics won't be lifted above the locking of a mutex? If not, is there any wording that explicitly says that it's kosher for the compiler or CPU to do so? For example, take…
jacobsa
  • 5,719
  • 1
  • 28
  • 60
16
votes
6 answers

What does "release sequence" mean?

I don't understand, why will be problems without release sequence, if we have 2 threads in the example below. We have only 2 operations on the atomic variable count. count is decremented sequently as shown in the output. From C++ Concurrency in…
Ivan Kush
  • 2,958
  • 2
  • 23
  • 39
15
votes
3 answers

Atomic access to shared memory

I have a shared memory between multiple processes that interpets the memory in a certain way. Ex: DataBlock { int counter; double value1; double ... } What I want is for the counter to be updated/incremented atomically. And for a memory release…
excalibur
  • 924
  • 2
  • 11
  • 26
15
votes
2 answers

C++ How is release-and-acquire achieved on x86 only using MOV?

This question is a follow-up/clarification to this: Does the MOV x86 instruction implement a C++11 memory_order_release atomic store? This states the MOV assembly instruction is sufficient to perform acquire-release semantics on x86. We do not need…
user997112
  • 29,025
  • 43
  • 182
  • 361
15
votes
3 answers

Why does g++ still require -latomic

In 29.5 Atomic types of the C++ Standard November 2014 working draft it states: There is a generic class template atomic. The type of the template argument T shall be trivially copyable (3.9). [ Note: Type arguments that are not also statically…
MikeMB
  • 20,029
  • 9
  • 57
  • 102
15
votes
3 answers

Will two relaxed writes to the same location in different threads always be seen in the same order by other threads?

On the x86 architecture, stores to the same memory location have a total order, e.g., see this video. What are the guarantees in the C++11 memory model? More precisely, in -- Initially -- std::atomic x{0}; -- Thread 1 -- x.store(1,…
levzettelin
  • 2,600
  • 19
  • 32
15
votes
4 answers

Easiest way to implement shared integer counter in C++11 without mutexes:

Suppose we have the following code which counts the number of times something occurs: int i=0; void f() { // do stuff . . . if(something_happens) ++i; } int main() { std::vector threads; for(int j = 0; j<…
user14717
  • 4,757
  • 2
  • 44
  • 68
1 2
3
39 40