stdatomic

Issue using std::atomic_flag with worker thread

十年热恋 提交于 2019-12-01 09:30:56
sorry for the verbosity - I did my best to condense my code sample into a minimally functional class and main() method. I'm trying to use an atomic_flag to notify _rx() within my worker thread to quit when stop() is called. I believe the issue is in trying to create my worker thread, thread SanityTestThread(&SanityTest::_rx, *this); which somehow clashes with my atomic_flag code sample (does not compile): #include <cstdio> #include <chrono> #include <unistd.h> #include <atomic> #include <iostream> #include <thread> using namespace std; class SanityTest { public: SanityTest(){} void start();

Issue using std::atomic_flag with worker thread

守給你的承諾、 提交于 2019-12-01 06:50:42
问题 sorry for the verbosity - I did my best to condense my code sample into a minimally functional class and main() method. I'm trying to use an atomic_flag to notify _rx() within my worker thread to quit when stop() is called. I believe the issue is in trying to create my worker thread, thread SanityTestThread(&SanityTest::_rx, *this); which somehow clashes with my atomic_flag code sample (does not compile): #include <cstdio> #include <chrono> #include <unistd.h> #include <atomic> #include

Is mov + mfence safe on NUMA?

梦想的初衷 提交于 2019-12-01 06:15:01
I see that g++ generates a simple mov for x.load() and mov + mfence for x.store(y) . Consider this classic example: #include<atomic> #include<thread> std::atomic<bool> x,y; bool r1; bool r2; void go1(){ x.store(true); } void go2(){ y.store(true); } bool go3(){ bool a=x.load(); bool b=y.load(); r1 = a && !b; } bool go4(){ bool b=y.load(); bool a=x.load(); r2= b && !a; } int main() { std::thread t1(go1); std::thread t2(go2); std::thread t3(go3); std::thread t4(go4); t1.join(); t2.join(); t3.join(); t4.join(); return r1*2 + r2; } in which according to https://godbolt.org/z/APS4ZY go1 and go2 are

Is mov + mfence safe on NUMA?

老子叫甜甜 提交于 2019-12-01 05:32:31
问题 I see that g++ generates a simple mov for x.load() and mov + mfence for x.store(y) . Consider this classic example: #include<atomic> #include<thread> std::atomic<bool> x,y; bool r1; bool r2; void go1(){ x.store(true); } void go2(){ y.store(true); } bool go3(){ bool a=x.load(); bool b=y.load(); r1 = a && !b; } bool go4(){ bool b=y.load(); bool a=x.load(); r2= b && !a; } int main() { std::thread t1(go1); std::thread t2(go2); std::thread t3(go3); std::thread t4(go4); t1.join(); t2.join(); t3

GCC atomic shared_ptr implementation

旧街凉风 提交于 2019-11-30 20:01:57
问题 According to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57250 , GCC 4.9 has support for atomic shared_ptr operations. Using GCC 4.9.2, I'm able to compile a program that uses atomic shared_ptr . The -mcx16 flag is required, as the GCC implementation on x86_64 apparently requires cmpxchg16b , which makes sense as I would assume that an atomic operation on a shared_ptr would require atomically updating both the pointer itself and the reference count at the same time. However, when I try to

Acquire/release semantics with 4 threads

无人久伴 提交于 2019-11-30 06:30:27
I am currently reading C++ Concurrency in Action by Anthony Williams. One of his listing shows this code, and he states that the assertion that z != 0 can fire. #include <atomic> #include <thread> #include <assert.h> std::atomic<bool> x,y; std::atomic<int> z; void write_x() { x.store(true,std::memory_order_release); } void write_y() { y.store(true,std::memory_order_release); } void read_x_then_y() { while(!x.load(std::memory_order_acquire)); if(y.load(std::memory_order_acquire)) ++z; } void read_y_then_x() { while(!y.load(std::memory_order_acquire)); if(x.load(std::memory_order_acquire)) ++z;

What exact rules in the C++ memory model prevent reordering before acquire operations?

守給你的承諾、 提交于 2019-11-29 16:47:28
问题 I have a question regarding the order of operations in the following code: std::atomic<int> x; std::atomic<int> y; int r1; int r2; void thread1() { y.exchange(1, std::memory_order_acq_rel); r1 = x.load(std::memory_order_relaxed); } void thread2() { x.exchange(1, std::memory_order_acq_rel); r2 = y.load(std::memory_order_relaxed); } Given the description of std::memory_order_acquire on the cppreference page (https://en.cppreference.com/w/cpp/atomic/memory_order), that A load operation with this

Why does g++ still require -latomic

纵饮孤独 提交于 2019-11-29 09:28:55
In 29.5 Atomic types of the C++ Standard November 2014 working draft it states: There is a generic class template atomic. The type of the template argument T shall be trivially copyable (3.9). [ Note: Type arguments that are not also statically initializable may be difficult to use. —end note ] So - as far as I can tell - this: #include <atomic> struct Message { unsigned long int a; unsigned long int b; }; std::atomic<Message> sharedState; int main() { Message tmp{1,2}; sharedState.store(tmp); Message tmp2=sharedState.load(); } should be perfectly valid standard c++14 (and also c++11) code.

What does “release sequence” mean?

半城伤御伤魂 提交于 2019-11-29 09:16:17
问题 I don't understand, why will be problems without release sequence , if we have 2 threads in the example below. We have only 2 operations on the atomic variable count . count is decremented sequently as shown in the output. From C++ Concurrency in Action by Antony Williams : I mentioned that you could get a synchronizes-with relationship between a store to an atomic variable and a load of that atomic variable from another thread, even when there’s a sequence of read-modify-write operations

Why does a std::atomic store with sequential consistency use XCHG?

烈酒焚心 提交于 2019-11-29 08:26:34
Why is std::atomic 's store : std::atomic<int> my_atomic; my_atomic.store(1, std::memory_order_seq_cst); doing an xchg when a store with sequential consistency is requested? Shouldn't, technically, a normal store with a read/write memory barrier be enough? Equivalent to: _ReadWriteBarrier(); // Or `asm volatile("" ::: "memory");` for gcc/clang my_atomic.store(1, std::memory_order_acquire); I'm explicitly talking about x86 & x86_64. Where a store has an implicit acquire fence. mov -store + mfence and xchg are both valid ways to implement a sequential-consistency store on x86. The implicit lock