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

Can std::atomic be safely used with OpenMP

I'm currently trying to learn ow to use OpenMP and I have a question. Is it safe to do something like that : std::atomic result; #pragma omp parallel for for(...) { result+= //some stuff; } Or shall I use : double result; …
Davidbrcz
  • 2,335
  • 18
  • 27
12
votes
1 answer

When should std::atomic_compare_exchange_strong be used?

There are two atomic CAS operations in C++11: atomic_compare_exchange_weak and atomic_compare_exchange_strong. According to cppreference: The weak forms of the functions are allowed to fail spuriously, that is, act as if *obj != *expected even if…
user571470
  • 394
  • 4
  • 14
11
votes
2 answers

How do atomics larger than the CPU's native support work

With current C++ compilers you can have atomic support of atomics that are larger than the actual support of your CPU. With x64 you can have atomics that are 16 bytes, but std::atomic also works with larger tuples. Look at this code: #include…
11
votes
1 answer

Why does std::atomic compile from C++17 even with a deleted copy constructor?

I have a simple code: #include int main() { std::atomic a = 0; } This code compiles fine with GCC 11.1.0 with -std=c++17, but fails with -std=c++14 and -std=c++11. using a deleted function std::atomic::atomic(const…
Michal
  • 627
  • 3
  • 13
11
votes
4 answers

Why is std::atomic::is_lock_free() not static as well as constexpr?

Can anyone tell me whether std::atomic::is_lock_free() isn't static as well as constexpr? Having it non-static and / or as non-constexpr doesn't make sense for me. Why wasn't it designed like C++17's is_always_lock_free in the first place?
Bonita Montero
  • 2,817
  • 9
  • 22
11
votes
1 answer

Why don't standard libraries implement std::atomic for structs under 8 bytes in a lock-free manner?

Assuming that the architecture can support 8 byte scalars in a lock free manner for std::atomic. Why don't standard libraries provide similar specializations for structs that are under 8 bytes? A simple implementation of such an std::atomic…
Curious
  • 20,870
  • 8
  • 61
  • 146
11
votes
2 answers

Assigning pointers to atomic type to pointers to non atomic type

Is the behavior of this code well-defined? #include const int test = 42; const int * _Atomic atomic_int_ptr; atomic_init(&atomic_int_ptr, &test); const int ** int_ptr_ptr = &atomic_int_ptr; printf("int = %d\n", **int_ptr_ptr);…
Some Name
  • 8,555
  • 5
  • 27
  • 77
11
votes
1 answer

Do I need std::atomic or is POD bool good enough?

Consider this code: // global std::atomic run = true; // thread 1 while (run) { /* do stuff */ } // thread 2 /* do stuff until it's time to shut down */ run = false; Do I need the overhead associated with the atomic variable here? My…
John S
  • 3,035
  • 2
  • 18
  • 29
10
votes
1 answer

Is the transformation of fetch_add(0, memory_order_relaxed/release) to mfence + mov legal?

The paper N4455 No Sane Compiler Would Optimize Atomics talks about various optimizations compilers can apply to atomics. Under the section Optimization Around Atomics, for the seqlock example, it mentions a transformation implemented in LLVM, where…
10
votes
1 answer

C11 Atomic Acquire/Release and x86_64 lack of load/store coherence?

I am struggling with Section 5.1.2.4 of the C11 Standard, in particular the semantics of Release/Acquire. I note that https://preshing.com/20120913/acquire-and-release-semantics/ (amongst others) states that: ... Release semantics prevent memory…
Chris Hall
  • 1,707
  • 1
  • 4
  • 14
10
votes
2 answers

Guarantee function call in logical AND expression

I am in the process of refactoring some code using C++ atomics. The code looks like this: std::atomic someFlag{}; // This can be set to true using a public method // ... const bool cond1 { someFunction() }; const bool cond2 { otherFunction()…
op414
  • 582
  • 5
  • 15
10
votes
3 answers

Does atomic read guarantees reading of the latest value?

In C++ we have keyword volatile and atomic class. Difference between them is that volatile does not guarantee thread-safe concurrent reading and writing, but just ensures that compiler will not store variable's value in cache and instead will load…
TwITe
  • 400
  • 2
  • 14
10
votes
1 answer

Can std::atomic memory barriers be used to transfer non-atomic data between threads?

Is the following code standards compliant? (or can it be made compliant without making x atomic or volatile?) This is similar to an earlier question, however I would like a citation to the relevant section of the C++ standard, please. My concern is…
Ross Bencina
  • 3,822
  • 1
  • 19
  • 33
10
votes
2 answers

How to perform basic operations with std::atomic when the type is not Integral?

To be precise, I only need to increase a double by another double and want it to be thread safe. I don't want to use mutex for that since the execution speed would dramatically decrease.
Noozen
  • 379
  • 3
  • 13
9
votes
1 answer

Transitivity of release-acquire

Just when I thought I got some grip around atomics, I see another article. This is an excerpt from GCC wiki, under Overall Summary: -Thread 1- -Thread 2- -Thread 3- y.store (20); if (x.load() == 10) { if (y.load()…
Jeenu
  • 2,109
  • 2
  • 20
  • 27