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

What's the difference between T, volatile T, and std::atomic?

Given the following sample that intends to wait until another thread stores 42 in a shared variable shared without locks and without waiting for thread termination, why would volatile T or std::atomic be required or recommended to guarantee…
horstr
  • 2,377
  • 12
  • 22
7
votes
1 answer

Do I need an atomic if a value is only written?

Suppose I have several threads accessing the same memory location. And, if at all, they all write the same value and none of them reads it. After that, all threads converge (through locks) and only then I read the value. Do I need to use an atomic…
Dany Bittel
  • 159
  • 1
  • 9
7
votes
1 answer

Is there a function to load a non-atomic value atomically?

In C++20 we can write: double x; double x_value = std::atomic_ref(x).load(); Is there a function with the same effect? I have tried std::atomic_load but there seem to be no overloads for non-atomic objects.
Pilar Latiesa
  • 675
  • 4
  • 10
7
votes
1 answer

Are memory orderings: consume, acq_rel and seq_cst ever needed on Intel x86?

C++11 specifies six memory orderings: typedef enum memory_order { memory_order_relaxed, memory_order_consume, memory_order_acquire, memory_order_release, memory_order_acq_rel, memory_order_seq_cst }…
user997112
  • 29,025
  • 43
  • 182
  • 361
7
votes
2 answers

How C++ Standard prevents deadlock in spinlock mutex with memory_order_acquire and memory_order_release?

TL:DR: if a mutex implementation uses acquire and release operations, could an implementation do compile-time reordering like would normally be allowed and overlap two critical sections that should be independent, from different locks? This would…
Alex Guteniev
  • 12,039
  • 2
  • 34
  • 79
7
votes
1 answer

Why does std::atomic give trivially copyable error?

My program is simple and I want to use atomic type. It works with int or double but it doesn't work with std::string. #include #include #include int main() { std::atomic test(0); // works std::cout<
gameon67
  • 3,981
  • 5
  • 35
  • 61
7
votes
2 answers

Does this envelope implementation correctly use C++11 atomics?

I have written a simple 'envelope' class to make sure I understand the C++11 atomic semantics correctly. I have a header and a payload, where the writer clears the header, fills in the payload, then fills the header with an increasing integer. The…
Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165
7
votes
1 answer

How do you create a std::pair containing a std::atomic?

I cannot figure out how to create the following: std::pair, int> I always invariably get /usr/include/c++/5.5.0/bits/stl_pair.h:139:45: error: use of deleted function 'std::atomic::atomic(const std::atomic&)' : first(__x),…
Rick D
  • 173
  • 4
7
votes
1 answer

Any disadvantages for std::atomic_flag not providing load or store operations? (Spin-lock example)

Comparing a std::atomic_flag to an std::atomic_bool (aka std::atomic), it seems to me that a std::atomic_flag just has a simpler interface. It provides only testing+setting and clearing the flag while an std::atomic_bool also provides…
j00hi
  • 5,420
  • 3
  • 45
  • 82
7
votes
1 answer

std::atomic in a union with another character

I recently read some code that had an atomic and a character in the same union. Something like this union U { std::atomic atomic; char character; }; I am not entirely sure of the rules here, but the code comments said that since a…
Curious
  • 20,870
  • 8
  • 61
  • 146
7
votes
1 answer

std::memory_order_relaxed atomicity with respect to the same atomic variable

The cppreference documentation about memory orders says Typical use for relaxed memory ordering is incrementing counters, such as the reference counters of std::shared_ptr, since this only requires atomicity, but not ordering or synchronization…
Curious
  • 20,870
  • 8
  • 61
  • 146
7
votes
3 answers

Approach of using an std::atomic compared to std::condition_variable wrt pausing & resuming an std::thread in C++

This is a separate question but related to the previous question I asked here I am using an std::thread in my C++ code to constantly poll for some data & add it to a buffer. I use a C++ lambda to start the thread like this: StartMyThread() { …
TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
7
votes
1 answer

The difference btween std::atomic and std::mutex

how to use std::atomic<> In the question above, obviously we can just use std::mutex to keep thread safety. I want to know when to use which one. classs A { std::atomic x; public: A() { x=0; } void Add() { …
Yves
  • 11,597
  • 17
  • 83
  • 180
7
votes
3 answers

Implementing atomic::store

I'm attempting to implement the atomic library from the C++0x draft. Specifically, I'm implementing §29.6/8, the store method: template void atomic::store(T pDesired, memory_order pOrder = memory_order_seq_cst); The requirement…
Joel Barba
  • 73
  • 3
7
votes
3 answers

sequenced-before modification order consistency

from http://en.cppreference.com : Relaxed ordering Atomic operations tagged std::memory_order_relaxed are not synchronization operations, they do not order memory. They only guarantee atomicity and modification order consistency. For example, with x…