Questions tagged [relaxed-atomics]

29 questions
23
votes
3 answers

How can memory_order_relaxed work for incrementing atomic reference counts in smart pointers?

Consider the following code snippet taken from Herb Sutter's talk on atomics: The smart_ptr class contains a pimpl object called control_block_ptr containing the reference count refs. // Thread A: // smart_ptr copy ctor smart_ptr(const smart_ptr&…
CppNoob
  • 2,322
  • 1
  • 24
  • 35
16
votes
6 answers

C++ standard: can relaxed atomic stores be lifted above a mutex lock?

Is there any wording in the standard that guarantees that relaxed stores to atomics won't be lifted above the locking of a mutex? If not, is there any wording that explicitly says that it's kosher for the compiler or CPU to do so? For example, take…
jacobsa
  • 5,719
  • 1
  • 28
  • 60
13
votes
3 answers

Is it possible that a store with memory_order_relaxed never reaches other threads?

Suppose I have a thread A that writes to an atomic_int x = 0;, using x.store(1, std::memory_order_relaxed);. Without any other synchronization methods, how long would it take before other threads can see this, using…
Carlo Wood
  • 5,648
  • 2
  • 35
  • 47
12
votes
2 answers

Does C++11 guarantee memory ordering between a release fence and a consume operation?

Consider the following code: struct payload { std::atomic< int > value; }; std::atomic< payload* > pointer( nullptr ); void thread_a() { payload* p = new payload(); p->value.store( 10, std::memory_order_relaxed ); …
11
votes
2 answers

std::atomic_bool for cancellation flag: is std::memory_order_relaxed the correct memory order?

I have a thread that reads from a socket and generates data. After every operation, the thread checks a std::atomic_bool flag to see if it must exit early. In order to cancel the operation, I set the cancellation flag to true, then call join() on…
Paul Belanger
  • 2,354
  • 14
  • 23
11
votes
1 answer

Understanding memory_order_relaxed

I am trying to understand the specifics of memory_order_relaxed. I am referring to this link : CPP Reference. #include #include std::atomic ptr {nullptr}; void fun1(){ ptr.store(new int{0},…
MS Srikkanth
  • 3,829
  • 3
  • 19
  • 33
10
votes
3 answers

Lock-free stack - Is this a correct usage of c++11 relaxed atomics? Can it be proven?

I've written a container for a very simple piece of data that needs to be synchronized across threads. I want the top performance. I don't want to use locks. I want to use "relaxed" atomics. Partly for that little bit of extra oomph, and partly to…
Michael Gazonda
  • 2,720
  • 1
  • 17
  • 33
9
votes
1 answer

Will fetch_add with relaxed memory order return unique values?

Imagine N threads running following simple code: int res = num.fetch_add(1, std::memory_order_relaxed); where num is: std::atomic num = 0; Is it completelly safe to assume, that res for each thread running the code will be different or it is…
bartop
  • 9,971
  • 1
  • 23
  • 54
9
votes
3 answers

Does atomic_thread_fence(memory_order_seq_cst) have the semantics of a full memory barrier?

A full/general memory barrier is one where all the LOAD and STORE operations specified before the barrier will appear to happen before all the LOAD and STORE operations specified after the barrier with respect to the other components of the…
Eric Z
  • 14,327
  • 7
  • 45
  • 69
7
votes
1 answer

Is the value of steady_clock::now from multiple threads consistent with memory order?

Within one thread, steady_clock::now() is guaranteed to return monotonically increasing values. How does this interact with memory ordering and reads observed by multiple threads? atomic arg{0}; steady_clock::time_point a, b, c, d; int…
Filipp
  • 1,843
  • 12
  • 26
6
votes
3 answers

Why does memory_order_relaxed use atomic (lock-prefixed) instructions on x86?

On Visual C++ 2013, when I compile the following code #include int main() { std::atomic v(2); return v.fetch_add(1, std::memory_order_relaxed); } I get back the following assembly on x86: 51 push ecx B8…
user541686
  • 205,094
  • 128
  • 528
  • 886
5
votes
0 answers

What are the optimal std::memory_orders for this scenario of a coroutine waiting on an event?

I'm working on a coroutine multiple-producers, single-consumer Event (here it is, for context). Simplified: class WaitList { public: void Append() { coro_.store(GetCurrentCoro()); } void Remove() { coro_.store({}); } void WakeUp() { auto…
Anton3
  • 577
  • 4
  • 14
5
votes
1 answer

Why isn't [[carries_dependency]] the default in C++?

I know that memory_order_consume has been deprecated, but I'm trying to understand the logic that went into the original design and how [[carries_dependency]] and kill_dependency were supposed to work. For that, I would like a specific example of…
user3188445
  • 4,062
  • 16
  • 26
4
votes
1 answer

Using relaxed atomic boolean to synchronize two threads

Some colleagues and I are having a discussion about a relaxed atomic boolean that is used to synchronize two threads. We have done some online research and found other samples and snippets dealing with relaxed atomics but we fail to draw any…
Maarten Bamelis
  • 2,243
  • 19
  • 32
4
votes
3 answers

Does calling `into_inner()` on an atomic take into account all the relaxed writes?

Does into_inner() return all the relaxed writes in this example program? If so, which concept guarantees this? extern crate crossbeam; use std::sync::atomic::{AtomicUsize, Ordering}; fn main() { let thread_count = 10; let…
Alexander Torstling
  • 18,552
  • 7
  • 62
  • 74
1
2