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

How to return a struct with atomic member in C++

Currently I have to work with legacy code and there is this struct: typedef struct somesome { int val; std:atomic index; } somesome; which is used inside this class: class Blub { protected: somesome blobb; public: ... somesome…
0xhph
  • 1
0
votes
1 answer

Which types of memory_order should be used for non-blocking behaviour with an atomic_flag?

I'd like, instead of having my threads wait, doing nothing, for other threads to finish using data, to do something else in the meantime (like checking for input, or re-rendering the previous frame in the queue, and then returning to check to see if…
0
votes
1 answer

atomic operations and visibility in C++

Consider the below code: static std::atomic x; // Thread 1 x.store(7, std::memory_order_relaxed); // Thread 2 x.load(std::memory_order_relaxed); Further assume that Thread 2 executed the load a few cycles after Thread 1 executed the…
cmutex
  • 1,478
  • 11
  • 24
0
votes
0 answers

Force release and acquire ordering for fetch and add with load/store

So I am trying to implement writer and reader methods in such a way where I always want the reader to read the value that was last written by the writer, and thus have reads ordered after writes. For this specific example, I am trying to use a very…
Josh Weinstein
  • 2,788
  • 2
  • 21
  • 38
0
votes
1 answer

shared_lock implemented with std::atomic

I can't afford to hold shared_lock due to its weight. Instead I implemented what I think is shared_lock but with atomic value. Will this code work, or have I missed something? update: I added RAII classes around atomic mutex. namespace atm { static…
vamirio-chan
  • 339
  • 1
  • 13
0
votes
0 answers

How to store multiple atomic variables?

I am writing a C++ program and am currently having an issue where I need atomics, preferably std::atomic_flag, to have thread safe flags. The issue is that I need multiple different atomic flags and rather than declaring them manually like…
Custos
  • 68
  • 6
0
votes
2 answers

Difference between atomic and int

I want to know if there is any different between std::atomic and int if we are just doing load and store. I am not concerned about the memory ordering. For example consider the below code int x{1}; void f(int myid) { while(1){ …
Gtrex
  • 247
  • 1
  • 7
0
votes
1 answer

What lock-free primitives do people actually use to do lock-free audio processing in c++?

Anyone who has done a bit of low level audio programming has been cautioned about locking the audio thread. Here's a nice article on the subject. But it's very unclear to me how to actually go about making a multithreaded audio processing…
Chet
  • 1,209
  • 1
  • 11
  • 29
0
votes
1 answer

Lockless queue using std::atomic

I wish to create a lockless queue using std::atomic. Here's my probably not so good first attempt at trying to do so: template class atomic_queue { public: using value_type = T; private: struct node { value_type…
Jorayen
  • 1,737
  • 2
  • 21
  • 52
0
votes
0 answers

std::atomic segmentation fault on WSL Ubuntu 20.04 LTS with GCC 9.x

Based on this question, I extended std::mutex so that I can check if the mutex is already locked by the current thread: class mutex : public std::mutex { public: void lock() { std::mutex::lock(); lock_holder_ =…
Developer Paul
  • 1,502
  • 2
  • 13
  • 18
0
votes
0 answers

DPDK implementation of MPSC ring buffer

While going through the implementation of the DPDK MPSC (multi-produce & single-consumer) Ring Buffer API, i found the code to move the head of the producer for inserting new elements in the Ring buffer. The function is as follows : static…
Prateek
  • 11
  • 3
0
votes
2 answers

Are C++ atomics preemption safe?

From what I understand of atomics, they are special assembly instructions which guarantee that two processors in an SMP system cannot write to the same memory region at the same time. For example in PowerPC an atomic increment would look something…
tinkerbeast
  • 1,707
  • 1
  • 20
  • 42
0
votes
0 answers

When there's no ordering requirements, is volatile integer in C/C++ enough for read-write conflict?

If there is only read-write conflict on an integer, i.e., some threads are reading but only one thread is writing. There's no write-write conflict. Furthermore, there's no ordering requirements. That is to say, even atomic variables are used,…
user534498
  • 3,926
  • 5
  • 27
  • 52
0
votes
2 answers

Simple usage of std::atomic for sharing data between two threads

I have two threads that share a common variable. The code structure is basically this (very simplified pseudo code): static volatile bool commondata; void Thread1() { ... commondata = true; ... } void Thread2() { ... while…
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
0
votes
1 answer

C++11 atomic<>: only to be read/written with provided methods?

I wrote some multithreaded but lock-free code that compiled and apparently executed fine on an earlier C++11-supporting GCC (7 or older). The atomic fields were ints and so on. To the best of my recollection, I used normal C/C++ operations to…
Swiss Frank
  • 1,985
  • 15
  • 33