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
1 answer

std::atomic: Access violation writing location 0xFEEEEFEEE in Visual Studio 2013 (VC12) - does not occur when using std::atomic

The following minimal code example compiled in Debug mode for x64 and run in the Visual Studio 2013 debugger yields an Unhandled exception at ...: Access violation writing location 0xFEEEFEEE. When debugging I see that the access violation…
alex
  • 2,252
  • 4
  • 23
  • 34
0
votes
0 answers

std::atomic is not supported?

I am trying to use std::atomic<> with struct sigaction in std::vector but my test case fails with linker error in both Clang and GCC. Why is that? I all types were supported, even non-fundamental ones. #include #include #include…
wilx
  • 17,697
  • 6
  • 59
  • 114
0
votes
0 answers

Performance of SRWLock vs std::atomic_flag

I have a scenario where both SRWLock in conjunction with Condition Variable, and std::atomic_flag will fulfill my needs. Is there any reason to prefer one over the other if performance is of concern ? I have two threads which both read and write…
sanchit.h
  • 134
  • 10
0
votes
0 answers

Correct atomic memory order, when 2 threads update same atomic variable and both use the value in condition (C++11)

I have one atomic integer variable which has a minimum and maximum value. Two threads update the variable, one increments it and the other decrements it. If incrementing would increase the value over the maximum value, the thread blocks and waits…
latexi95
  • 31
  • 1
  • 2
0
votes
1 answer

Understanding why race condition happens when only one thread does the write operation

I recently asked "Thrown object cannot be caught in a multi-threaded solution" and got the correct answer which works perfectly. However, I am still confused why there can be a race condition when only one thread does the write operation. Let me…
Benji Mizrahi
  • 2,154
  • 2
  • 23
  • 38
0
votes
1 answer

What's the compative cost of an atomic RMW operation and a function call?

My understanding is that atomic machine instructions may be up to two orders of magnitude slower than a non-atomic operation. For example, given int x; x++ and std::atomic y; y++; my understanding is that x++ typically runs much faster than…
KnowItAllWannabe
  • 12,972
  • 8
  • 50
  • 91
0
votes
1 answer

Access violation when accessing std::atomic_ullong objects on visual studio 2012 under release mode

This is my code(removed some unreleated parts though): #include #include #include using namespace std; struct Profiler { private: Profiler() {} Profiler(const Profiler& other) {} Profiler operator = (const…
0
votes
2 answers

memory modeling test in c++11 , curious for memory_order_relaxed

I have read webpage : http://bartoszmilewski.com/2008/12/01/c-atomics-and-memory-ordering/ and then coding the test source compiled at g++ 4.8.1 , cpu is Intel ... global var : r1=0;r2=0;x=0;y=0; Thread1 : x = 1 ; //line 1 r1…
barfatchen
  • 1,630
  • 2
  • 24
  • 48
-1
votes
1 answer

How to synchronise constructor with the rest of the class

Say I define a toy class that I want to claim is thread-safe. (Added following long discussion in comments: This a thought experiment about a race that I believe is commonly present and largely benign, but nevertheless real. It is between the…
-1
votes
0 answers

Cannot build with std::atomic on Debian WSL

I am having trouble building a C++ project that uses std::atomic on Debian WSL. I am unable to reproduce on Godbolt. Building the below code snippet #include #include // packed struct definition #pragma pack(push, 1) struct…
k huang
  • 409
  • 3
  • 10
-1
votes
1 answer

`latomic` flag sometimes not required?

I know that the -latomic flag is required for the atomic library to be linked in by GCC. However, for some reason std::atomic doesn't require it to build build without latomic while structs do build requires latomic what is this difference…
k huang
  • 409
  • 3
  • 10
-1
votes
1 answer

Using Numerous Atomic Variables, to eliminate mutex(s), has unexplained Consequences

After 5 days working on this (3 major rewrites), I seek help from you wise people on StackOverflow. I’ve used atomic int(s) before, but not this extensively. THE SCENARIO; I have batches, 1..1000+ batches, of test labeled A..Z. Batches set 2, cant…
Adrian E
  • 29
  • 6
-1
votes
1 answer

Can we use Compare-and-Swap operation on non-atomic variable?

Hi I noticed that the CAS Compare-and-Swap operations are usually operated on atomic variables, see https://en.cppreference.com/w/cpp/atomic/atomic/compare_exchange, …
Dachuan Huang
  • 115
  • 1
  • 8
-1
votes
1 answer

Is my understanding of __ATOMIC_SEQ_CST correct? (I'd like to write a mutex with it + atomics)

For fun I'm writing my own threading library used by me and a friend or two. First thing I'd like to write is a mutex It appears I'm generating the assembly I want. __atomic_fetch_add seems to generate lock xadd and __atomic_exchange seems to…
Eric Stotch
  • 141
  • 4
  • 19
-1
votes
5 answers

Is it safe to write "y=++x" when using c++ atomic?

If I have 2 threads and in main function, I init a variable x like this std::atomic x=0;, In thread 1 I do this: while(true){ x++; } And In thread 2 I do this: y=++x; my question is that:is there any possibility that variable y can get the…
scirocc
  • 153
  • 6
1 2 3
39
40