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

Are memory orders for each atomic correct in this lock-free SPSC ring buffer queue?

I have a ring buffer that looks like: template class RingBuffer { public: bool Publish(); bool Consume(T& value); bool IsEmpty(std::size_t head, std::size_t tail); bool IsFull(std::size_t head, std::size_t tail); private: …
9
votes
2 answers

C++ atomics: how to allow only a single thread to access a function?

I'd like to write a function that is accessible only by a single thread at a time. I don't need busy waits, a brutal 'rejection' is enough if another thread is already running it. This is what I have come up with so far: std::atomic busy…
Ignorant
  • 2,411
  • 4
  • 31
  • 48
9
votes
3 answers

Is there any performance difference in just reading an atomic variable compared to a normal variable?

int i = 0; if(i == 10) {...} // [1] std::atomic ai{0}; if(ai == 10) {...} // [2] if(ai.load(std::memory_order_relaxed) == 10) {...} // [3] Is the statement [1] any faster than the statements [2] & [3] in a multithreaded…
iammilind
  • 68,093
  • 33
  • 169
  • 336
9
votes
3 answers

Why does default constructor of std::atomic not default initialize the underlying stored value?

Since it's Thanksgiving today in the USA, I'll be the designated turkey to ask this question: Take something as innocuous as this. An atomic with a simple plain old data type such as an int: atomic x; cout << x; The above will print out…
selbie
  • 100,020
  • 15
  • 103
  • 173
9
votes
1 answer

std::atomic memory_order_relaxed VS volatile sig_atomic_t in a multithreaded program

Does volatile sig_atomic_t give any memory order guarantees? E.g. if I need to just load/store an integer is it ok to use? E.g. here: volatile sig_atomic_t x = 0; ... void f() { std::thread t([&] {x = 1;}); while(x != 1) {/*waiting...*/} …
kan
  • 28,279
  • 7
  • 71
  • 101
9
votes
2 answers

is it ok to use std::atomic with a struct that is POD except that it has a construtor?

I am using a few atomic variables, all unsigned int's, and I wanted to collect them into a structure - effectively a POD. However I also want a constructor because my compiler is not quite c++11 (so I have to define my own constructor to create it…
code_fodder
  • 15,263
  • 17
  • 90
  • 167
9
votes
5 answers

reordering atomic operations in C++

Suppose I have 2 threads: int value = 0; std::atomic ready = false; thread 1: value = 1 ready = true; thread 2: while (!ready); std::cout << value; Is this program able to output 0? I read about the C++ memory model - specifically,…
matts1
  • 857
  • 1
  • 9
  • 20
9
votes
3 answers

Memory Model in C++ : sequential consistency and atomicity

I have some questions connected with memory model in C++11. On https://www.think-cell.com/en/career/talks/pdf/think-cell_talk_memorymodel.pdf on the 29. slide is written The C++ memory model guarantees sequential consistency But, in my previous…
Gilgamesz
  • 4,727
  • 3
  • 28
  • 63
9
votes
0 answers

How do you use std::atomic to achieve thread-safety without locking mutexes?

I know, that in some situations you can avoid having to lock mutexes (std::mutex) by using std::atomic, thus increasing performance. Can you name a situation like this, and preferably show some example code on how to do this (how do you use…
krispet krispet
  • 1,648
  • 1
  • 14
  • 25
9
votes
2 answers

Does the MOV x86 instruction implement a C++11 memory_order_release atomic store?

According to this https://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html, a released store is implemented as MOV (into memory) on x86 (including x86-64). According to his http://en.cppreference.com/w/cpp/atomic/memory_order memory_order_release: A…
Krab
  • 6,526
  • 6
  • 41
  • 78
9
votes
3 answers

Does atomic_thread_fence(memory_order_seq_cst) have the semantics of a full memory barrier?

A full/general memory barrier is one where all the LOAD and STORE operations specified before the barrier will appear to happen before all the LOAD and STORE operations specified after the barrier with respect to the other components of the…
Eric Z
  • 14,327
  • 7
  • 45
  • 69
9
votes
3 answers

std::atomic not supported by clang?

I am trying to use std::atomic with clang. However, whenever I try to include the header file atomic (#include ), I get the message "atomic not found". Note that I'm including -std=c++11 -stdlib=libc++ while compiling. What am I missing? The…
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356
9
votes
4 answers

C11/C++11 weak memory benchmarks

Can anyone point to benchmark results comparing performance of C11/C++11 code using relaxed atomic operations (particularly memory_order_release and memory_order_acquire, but also memory_order_consume and memory_order_relaxed) versus the default…
user2949652
  • 563
  • 2
  • 5
9
votes
2 answers

c++ 11 std::atomic_flag, am I using this correctly?

I have a simple boolean value I need to test and set in a thread-safe manner. If one thread is already working, I want the second thread to exit. If I understand std::atomic_flag correctly, this should work fine. However, I'm not confident I…
Tom
  • 1,977
  • 1
  • 20
  • 19
8
votes
1 answer

Changing code from Sequential Consistency to a less stringent ordering in a barrier implementation

I came across this code for a simple implementation of a barrier (for code that can't use std::experimental::barrier in C++17 or std::barrier in C++20) in C++ Concurrency in Action book. [Edit] A barrier is a synchronization mechanism where a group…
user17799869
  • 125
  • 9