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

Are relaxed atomic store reordered themselves before the release? (similar with load /acquire)

I read in the en.cppreference.com specifications relaxed operations on atomics: "[...]only guarantee atomicity and modification order consistency." So, I was asking myself if such 'modification order' would work when you are working on the same…
0
votes
1 answer

Implementation of a singleton class with atomic members

I'm trying to implement a class to track the performances of a multithreaded code and am running into the following issue. The tracker should have a single instance, thus the singleton pattern. In order to count object creation and function…
0
votes
2 answers

How to let different threads fill an array together?

Suppose I have some tasks (Monte Carlo simulations) that I want to run in parallel. I want to complete a given number of tasks, but tasks take different amount of time so not easy to divide the work evenly over the threads. Also: I need the results…
willem
  • 2,617
  • 5
  • 26
  • 38
0
votes
1 answer

Trying to control multithreaded access to array using std::atomic

I'm trying to control multithreaded access to a vector of data which is fixed in size, so threads will wait until their current position in it has been filled before trying to use it, or will fill it themselves if no-one else has yet. (But ensure…
Tyler Shellberg
  • 1,086
  • 11
  • 28
0
votes
1 answer

Updating an atomic variable with a non atomic and vice versa

I am updating an atomic variable size_t using from one thread and reading it from another. Following is the code: Code: // MyClass.hpp #pragma once #include class MyClass { public: size_t GetVal() { return m_atomic_val; } …
AdeleGoldberg
  • 1,289
  • 3
  • 12
  • 28
0
votes
1 answer

How do we force variable sharing?

Consider the following code: std::atomic flag(false); //Thread 1 flag.store(true,std::memory_order_relaxed); //Thread 2 while(!flag.load(std::memory_order_relaxed)) ; // stay in the loop std::cout << "loaded"; Is there any gurantee that the…
Koosha
  • 1,492
  • 7
  • 19
0
votes
1 answer

std::memory_order_acquire fence necessary on x86?

Given x86 has a strong memory model, is std::memory_order_acquire fence (not operation) necessary? For example, if I have this code: uint32_t read_shm(const uint64_t offset) { // m_head_memory_location is char* pointing to the beginning of a…
HCSF
  • 2,387
  • 1
  • 14
  • 40
0
votes
1 answer

is `memory_order_relaxed` necessary to prevent partial reads of atomic stores

Suppose thread 1 is doing atomic stores on a variable v using memory_order_release (or any other order) and thread 2 is doing atomic reads on v using memory_order_relaxed. It should be impossible to have partial reads in this case. An example of…
user869887
  • 198
  • 2
  • 6
0
votes
1 answer

Atomically incrementing an integer in shared memory for multiple processes on linux x86-64 with gcc

The Question What's a good way to increment a counter and signal once that counter reaches a given value (i.e., signaling a function waiting on blocks until full, below)? It's a lot like asking for a semaphore. The involved processes are…
interestedparty333
  • 2,386
  • 1
  • 21
  • 35
0
votes
2 answers

Avoiding deadlock in concurrent waiting object

I've implemented a "Ticket" class which is shared as a shared_ptr between multiple threads. The program flow is like this: parallelQuery() is called to start a new query job. A shared instance of Ticket is created. The query is split into multiple…
benjist
  • 2,740
  • 3
  • 31
  • 58
0
votes
2 answers

What operations on a c++ 11 atomic variable are actually atomic?

I'm reading the cpp reference manual on std::atomic (https://en.cppreference.com/w/cpp/atomic/atomic) and I'm confused about the exact operations that are actually executed atomically. The main operations I'm having trouble with: operator=,…
EliT
  • 27
  • 8
0
votes
2 answers

C++11 memory_model_relaxed and memory_order_seq_cst relation

Hi I am trying to understand lock-free work stealing dequeue implementation. Currently, I am reading one implementation by google's filament here. I am mostly concerned about the steal operation. template TYPE…
kevinyu
  • 1,367
  • 10
  • 12
0
votes
0 answers

atomic store compilation problem on linux

The code #include #include #include #include #include #include using namespace std; static shared_ptr > m1; //static atomic > > m3; int…
Raj
  • 401
  • 6
  • 20
0
votes
0 answers

How to correctly use an atomic type with condition_variable to avoid locking during relaxed atomic reads

In the case of a shared counter that is waitable (with condition_variable) I wonder what is best regarding the count getter between using an atomic type for the count (mt_counter_B impl) or locking (mt_counter_A impl) ? Also, are the memory_orders…
0
votes
1 answer

Does a process switch affect std::atomic compare and exchange in arm9 processor?

I am new to std::atomic in c++ and trying to understand the implementation of compare and exchange operations under ARM processors.I am using gcc on linux. When i look into the assembly code mcr p15, 0, r0, c7, c10, 5 .L41: ldrexb r3, [r2] …
aravind b
  • 1
  • 3