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 get value of atomic variable outside the thread in C++

I have a thread, which keeps running, and this thread receives data from udp network. when there is no data coming in for lets say 3 seconds, i am setting a atomic variable[STATUS] as 0 and if data started coming again setting atomic…
Sara
  • 189
  • 1
  • 13
0
votes
2 answers

std:atomic initialized with reference creates copy of underlying variable?

Let's consider next snippet: int val=5; int& ref=val; std::atomic atomicref(ref); ++atomicref; std::cout<< "atomic ref="<
BorisV
  • 683
  • 3
  • 20
0
votes
0 answers

the result dual store - load atomic operation

At start a and b are 0 In thread 1,atomic write a=1 then atomic read b In thread 2,atomic write b=1 then atomic read a If I want to make sure thread 1 can read b=1 or thread 2 can read a=1,which memory order should I use,and why?
0
votes
1 answer

Trie structure, lock-free inserting

I tried to implement lock free Trie structure, but I am stuck on inserting nodes. At first I believed it was easy (my trie structure would not have any delete methods) but even swapping one pointer atomically can be tricky. I want to swap pointer to…
dandon223
  • 5
  • 5
0
votes
1 answer

std::atomic: Does the memory barrier hold up when task loops around?

So say I have this construct where two Tasks run at the same time (pseudo code): int a, b, c; std::atomic flag; TaskA() { while (1) { a = 5; b = 2; c = 3; flag.store(true, std::memory_order_release); …
glades
  • 3,778
  • 1
  • 12
  • 34
0
votes
0 answers

C++ atomic fences and reordering

I have a question regarding the following code: std::shared_ptr shared_obj = std::make_shared(222); std::atomic flag = false; ... THREAD 1 while (running.load(std::memory_order_relaxed)) { if (std::shared_ptr v =…
0
votes
0 answers

Atomic member variable

We are supposed to declare the private member variable trackerCoordinates as an atomic. The .h was originally written like: class Missile : public Attack, public Sea::Coordinates { private: bool hit = false; …
0
votes
0 answers

Can we make S[x++] = str atomic using std::atomic?

I have the following singleton class: class RecordKeeper { public: ... RecordKeeper* get(); void addRecord(std::string &rec); private: std::string records[MAX]; // this has to be a fixed size circular array int len; }; void…
Sajib
  • 404
  • 3
  • 15
0
votes
1 answer

Can we use c++20's atomic_ref to concurrently accessing user type member function?

Consider this problem: I have an atomic_ref of a user type. I want to concurrently accessing its member functions. See code below: struct A{ int counter=0; int add(){ ++counter; return counter; }; }; int main() { …
kilasuelika
  • 165
  • 6
0
votes
1 answer

Is 11 a valid output under ISO c++ for x86_64,arm or other arch?

This question is based on Can't relaxed atomic fetch_add reorder with later loads on x86, like store can? I agree with answer given. On x86 00 will never occur because a.fetch_add has a lock prefix/full barrier and loads can't reorder above…
nvn
  • 157
  • 1
  • 5
0
votes
1 answer

Output 11 for this program never occurs

This time i use atomic_fetch_add . Here is how i can get ra1=1 and ra2=1 . Both the threads see a.fetch_add(1,memory_order_relaxed); when a=0. The writes go into store buffer and isn't visible to the other. Both of them have ra=1 and ra2=1. I can…
nvn
  • 157
  • 1
  • 5
0
votes
2 answers

Multithreading atomics a b printing 00 for memory_order_relaxed

In the below code the write to a in foo is stored in store buffer and not visible to the ra in bar. Similarly the write to b in bar is not visible to rb in foo and they print 00. // g++ -O2 -pthread axbx.cpp ; while [ true ]; do ./a.out | grep…
nvn
  • 157
  • 1
  • 5
0
votes
2 answers

output 10 with memory_order_seq_cst

When i run this program i get output as 10 which seems to be impossible for me. I'm running this on x86_64 core i3 ubuntu. If the output is 10, then 1 must have come from either c or d. Also in thread t[0], we assign c as 1. Now a is 1 since it…
nvn
  • 157
  • 1
  • 5
0
votes
0 answers

Does atomic variable's access after mutex need memory order?

I have 2 threads, one atomic variable A and a mutex L: thread 1: void changeA(val) { // Will monotonically increase A L.lock(); int oldVal = A.load(std::memory_order_relaxed); A.store(max(val, oldVal), std::memory_order_relaxed); // Do…
Quyen Le
  • 41
  • 1
  • 6
0
votes
0 answers

I'm getting an error when trying to compile this constructor

I have a class with a constructor but the compilation fails with the error: error: use of deleted function 'std::atomic::atomic(const std::atomic&)' I'm struggling to understand what this error means. I've…
Charlie W
  • 1
  • 1
  • 1