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

Confusion about happens before relationship in concurrency

Below is an example given in Concurrency in Action , and the author says the assert may fire, but I don't understand why. #include #include #include std::atomic x,y; std::atomic z; void write_x_then_y() { …
choxsword
  • 3,187
  • 18
  • 44
0
votes
1 answer

Natural alignment + volatile = atomic in C++11?

1) Is the following declaration of a naturally aligned pointer: alignas(sizeof(void *)) volatile void * p; equivalent to std::atomic in C++11? 2) Saying more exactly, is it correct to assume that this type of pointer will work in the…
Alexey Starinsky
  • 3,699
  • 3
  • 21
  • 57
0
votes
1 answer

Strict conversion of std::atomic_bool values

I have a code with very simple logical arithmetic that involves values returned from std::atomic_bool. #include #include int main() { uint16_t v1 = 0x1122; uint16_t v2 = 0xaaff; std::atomic_bool flag1(false); …
ilya1725
  • 4,496
  • 7
  • 43
  • 68
0
votes
1 answer

Purpose of _Compiler_barrier() on 32bit read

I have been stepping through the function calls that are involved when I assign to an atomic_long type on VS2017 with a 64bit project. I specifically wanted to see what happens when I copy an atomic_long into a none-atomic variable, and if there is…
Wad
  • 1,454
  • 1
  • 16
  • 33
0
votes
1 answer

User defined atomic less than

I've been reading and it seems that std::atomic doesn't support a compare and swap of the less/greater than variant. I'm using OpenMP and need to safely update a global minimum value. I was thinking this would be as easy as using a built-in API. But…
Joey Carson
  • 2,973
  • 7
  • 36
  • 60
0
votes
0 answers

C++ Getting Error with Assignment operator for Atomic Members in class

Here is a Simple code to show my problem. #include #include using namespace std; class T { public: atomic a; }; int main() { int x; T X,Y; x = 10; X.a = x; Y = X; cout << Y.a; } I get the following error Message when I…
0
votes
2 answers

numeric_limits of atomic types

Suppose someAtomic is an std::atomic with an integral underlying type, such as atomic_uint16_t. I don't want to assume WHICH integral type, however, in particular code, so I want something to accomplish the following, which right now doesn't…
Display Name
  • 2,323
  • 1
  • 26
  • 45
0
votes
1 answer

Polling for a atomic variable (bool) in C++?

I have a thread which modifies one atomic variable (bool) and another thread which wants to poll that variable. My application makes few function calls in thread A and start polling on one specific atomic variable. Thread B keeps on reading the…
abhiarora
  • 9,743
  • 5
  • 32
  • 57
0
votes
2 answers

Correctness of using std::atomic in combination with std::mutex

I have written a shared priority queue class. To signal to stop serving data I use method Cancel(), which sets a sign done to false and application is not allowed to write / read any data from the queue. I am not sure about using std::atomic
Dom
  • 532
  • 1
  • 9
  • 23
0
votes
0 answers

C++ concurrency in action

This is a question about the memory order listed in 'c++ concurrency in action'. First, in listing 5.7; the consequence can be: Thread A : x.store(true, release) Thread B : x.load(acquire) returns true, y.load(acquire) returns false Thread C :…
최범석
  • 49
  • 4
0
votes
1 answer

atomic - Deleted Copy Constructor

A class, having an array of atomic objects as one of it's private data member, is desired. class A { vector> arr; public: A(int size, int init) : arr(vector>(size,init)) {} // Error: Deleted 'atomic(const…
stillanoob
  • 1,279
  • 2
  • 16
  • 29
0
votes
0 answers

How to make atomic getters and setters in a singleton?

I have a struct of getters and setters that I use to use std::unique_lock lock to access. all the getters shared the same lock which essentially made any request to the singleton to be serialized. The for mentioned solution worked but was slow. I…
noztol
  • 494
  • 6
  • 25
0
votes
0 answers

Is there an additional cost for std::atomic ?

I am trying to implement a lock free data structure for a project I am working on . It is a simulation with many parts in it (1 million > ) and I am trying to make the parts take up as small a size as possible ? By cost if you could provide more…
nnrales
  • 1,481
  • 1
  • 17
  • 26
0
votes
0 answers

How to to set/get the low bits of a pointer to a struct?

I am trying to work on a lockfree solution for a self organizing list. I recently found out about the tagged pointers and thought it could work for me. I have my node structure as follows: typedef struct node { unsigned long val; struct node *…
moriczCJ
  • 23
  • 3
0
votes
0 answers

Mix lock xadd with lock xchg. Can the store with xchg get lost?

In C++ code, but not specific to C++ and could just as easily be expressed in many languages: atomic counter(0); // Many threads counter.fetch_add(1); // lock xadd // One thread std::lock_guard
Eloff
  • 20,828
  • 17
  • 83
  • 112