relaxed-atomics

What is the (slight) difference on the relaxing atomic rules?

守給你的承諾、 提交于 2020-08-25 10:30:06
问题 After seeing Herb Sutters excellent talk about "atomic weapons" I got a bit confused about the Relaxed Atomics examples. I took with me that an atomic in the C++ Memory Model (SC-DRF = Sequentially Consistent for Data Race Free) does an "acquire" on a load/read. I understand that for a load [and a store] the default is std::memory_order_seq_cst and therefore the two are the same: myatomic.load(); // (1) myatomic.load(std::memory_order_seq_cst); // (2) So far so good, no Relaxed Atomics

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

非 Y 不嫁゛ 提交于 2020-01-12 14:26:07
问题 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 really understand them. I've been working on this a lot, and I'm at the point where this code passes all tests I throw at it. That's not quite "proof" though, and so I'm wondering if there's anything I'm missing, or any other ways I can test this?

Example of misuse of std::memory_order::relaxed in C++ Standard [algorithms.parallel.exec/5 in n4713]

不羁岁月 提交于 2020-01-03 21:04:15
问题 One of the examples of misuse of std::memory_order::relaxed in C++ Standard: std::atomic<int> x{0}; int a[] = {1,2}; std::for_each(std::execution::par, std::begin(a), std::end(a), [&](int) { x.fetch_add(1, std::memory_order::relaxed); // spin wait for another iteration to change the value of x while (x.load(std::memory_order::relaxed) == 1) { } // incorrect: assumes execution order }); And then it says, The above example depends on the order of execution of the iterations, and will not

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

笑着哭i 提交于 2019-12-21 04:31:22
问题 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 x.load(std::memory_order_relaxed); ? Is it possible that the value written to x stays entirely thread-local given the current definition of the C/C++ memory model that the standard gives? The practical case that I have at hand is where a thread B reads an atomic_bool frequently

When is it okay to do/use something that has unspecified behaviour? [duplicate]

丶灬走出姿态 提交于 2019-12-13 09:20:45
问题 This question already has answers here : Undefined, unspecified and implementation-defined behavior (8 answers) Closed 5 years ago . In C++, there are things that come up that are somewhere between well-defined and undefined. Specifically, those are called implementation defined and unspecified . Right now, I'm interested in the unspecified stuff. When is it okay to use such features, and when should they be avoided? Are there good examples of unspecified behaviour being a part of correct

Understanding memory_order_relaxed

一笑奈何 提交于 2019-12-06 08:33:37
问题 I am trying to understand the specifics of memory_order_relaxed. I am referring to this link : CPP Reference. #include <future> #include <atomic> std::atomic<int*> ptr {nullptr}; void fun1(){ ptr.store(new int{0}, std::memory_order_relaxed); } void fun2(){ while(!ptr.load(std::memory_order_relaxed)); } int main(){ std::async(std::launch::async, fun1); std::async(std::launch::async, fun2); } Question 1: In the code above, is it technically possible for fun2 to be in an infinite loop where it

Understanding memory_order_relaxed

只谈情不闲聊 提交于 2019-12-04 14:38:38
I am trying to understand the specifics of memory_order_relaxed. I am referring to this link : CPP Reference . #include <future> #include <atomic> std::atomic<int*> ptr {nullptr}; void fun1(){ ptr.store(new int{0}, std::memory_order_relaxed); } void fun2(){ while(!ptr.load(std::memory_order_relaxed)); } int main(){ std::async(std::launch::async, fun1); std::async(std::launch::async, fun2); } Question 1: In the code above, is it technically possible for fun2 to be in an infinite loop where it sees the value of ptr as nullptr even if the thread that sets ptr has finished running? If suppose, I

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

我是研究僧i 提交于 2019-12-04 08:30:49
问题 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& other) { ... control_block_ptr = other->control_block_ptr; control_block_ptr->refs.fetch_add(1, memory_order_relaxed); ... } // Thread D: // smart_ptr destructor ~smart_ptr() { if (control_block_ptr->refs.fetch_sub(1, memory_order_acq_rel) == 0) {

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

一世执手 提交于 2019-12-02 23:29:43
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& other) { ... control_block_ptr = other->control_block_ptr; control_block_ptr->refs.fetch_add(1, memory_order_relaxed); ... } // Thread D: // smart_ptr destructor ~smart_ptr() { if (control_block_ptr->refs.fetch_sub(1, memory_order_acq_rel) == 0) { delete control_block_ptr; } } Herb Sutter says the increment of refs in Thread A can use memory_order