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

What is the difference between load/store relaxed atomic and normal variable?

As I see from a test-case: https://godbolt.org/z/K477q1 The generated assembly load/store atomic relaxed is the same as the normal variable: ldr and str So, is there any difference between relaxed atomic and normal variable?
LongLT
  • 411
  • 6
  • 19
8
votes
2 answers

Anything in std::atomic is wait-free?

If T is a C++ fundamental type, and if std::atomic::is_lock_free() returns true, then is there anything in std::atomic that is wait-free (not just lock-free)? Like, load, store, fetch_add, fetch_sub, compare_exchange_weak, and…
Koosha
  • 1,492
  • 7
  • 19
8
votes
1 answer

How to assign a vector of atomic types?

How can I assign the members of a vector with an atomic type? #include #include #include using namespace std; int main() { vector> myvector; int N=8; myvector.assign(N,false); …
ar2015
  • 5,558
  • 8
  • 53
  • 110
8
votes
1 answer

Why do GCC atomic builtins need an additional "generic" version?

According to https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html, there's: type __atomic_load_n (type *ptr, int memorder) and (the "generic"): void __atomic_load (type *ptr, type *ret, int memorder) then void __atomic_store_n (type…
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
8
votes
1 answer

How to multiply two values and store the result atomically?

Say I have the following global variables in my code: std::atomic x(...); std::atomic y(...); std::atomic z(...); My task is to multiply x and y and then store the result in z: z = x * y I'm aware that the naive…
Ignorant
  • 2,411
  • 4
  • 31
  • 48
8
votes
1 answer

Why does a std::atomic store with sequential consistency use XCHG?

Why is std::atomic's store: std::atomic my_atomic; my_atomic.store(1, std::memory_order_seq_cst); doing an xchg when a store with sequential consistency is requested? Shouldn't, technically, a normal store with a read/write memory barrier be…
Leandros
  • 16,805
  • 9
  • 69
  • 108
8
votes
1 answer

Why Visual C++ 2015 allows std::atomic assignment?

A few days ago I've written something like the following: struct A { std::atomic_bool b = false; }; Compiled in Visual Studio 2015 Update 3 with its VC++2015 compiler, nothing wrong popped up. Now I've recompiled the same thing with GCC (5.4.0)…
roalz
  • 2,699
  • 3
  • 25
  • 42
8
votes
2 answers

fetch_add with acq_rel memory order

Consider an std::atomic x(0); Let's suppose I have a function doing the following: int x_old = x.fetch_add(1,std::memory_order_acq_rel); Based on the description for acquire release memory ordering: memory_order_relaxed Relaxed operation:…
Aditya Sihag
  • 5,057
  • 4
  • 32
  • 43
8
votes
2 answers

Atomic decrement-and-test in C

I'm implementing a reference-counting system in C that needs to work with multiple threads. As a result, I need a way to decrement an integral reference count and test if the result is zero with one atomic operation. I can use C11 and stdatomic.h,…
user6149363
  • 153
  • 5
8
votes
1 answer

error C2280: attempting to reference a deleted function (atomic)

I have a class A with a member variable _atomicVar of type std::atomic. #include class A { public: A(); ~A(); private: std::atomic _atomicVar; }; If I build the project I get the following error: error C2280:…
TorbenJ
  • 4,462
  • 11
  • 47
  • 84
8
votes
1 answer

Xcode and C11 stdatomic.h

Seems like Xcode 5 and higher supports C11 but when I try to include stdatomic.h it says it can not find the file? Is it possible to use C11 atomic structures in Xcode?
bitwise
  • 541
  • 6
  • 16
8
votes
1 answer

C++ 11 undefined reference to `__atomic_store_16'

The following code fails to link: #include struct A { unsigned long a; …
user2026095
8
votes
2 answers

How do I initialize an array of std::atomics to zeros?

std::array< std::atomic_size_t, 10 > A; // ... std::atomic_init(A, {0}); // error A = {ATOMIC_VAR_INIT(0)}; // error How would you initialize an array of std::atomic to 0s? Even for loops updating one element of the array at every step does not…
Pippo
  • 1,543
  • 3
  • 21
  • 40
7
votes
1 answer

What does std::atomic::is_always_lock_free = true really mean?

I have the following code: #include int main () { std::atomic value(0); value.fetch_add(1, std::memory_order::relaxed); static_assert(std::atomic::is_always_lock_free); return 0; } It compiles and so it…
HCSF
  • 2,387
  • 1
  • 14
  • 40
7
votes
0 answers

What C++ construct should be used to access memory shared with another process

How should C++ code access memory that is shared with another application, e.g. through a memmapd file? The options seem to be: Regular raw accesses, e.g. int* p = ...address of shared memory...; *p = 5; *p. This seems unsafe because the compiler…
Gregory
  • 1,205
  • 10
  • 17