stdatomic

Is it safe to use the Structure dereference(->) operator on the result of std::atomic::load

浪尽此生 提交于 2020-08-04 10:20:08
问题 Whilst trying to work with std atomic pointer, I ran into the following. Say I do this: std::atomic<std::string*> myString; // <do fancy stuff with the string... also on other threads> //A can I do this? myString.load()->size() //B can I do this? char myFifthChar = *(myString.load()->c_str() + 5); //C can I do this? char myCharArray[255]; strcpy(myCharArray, myString.load()->c_str()); I'm pretty sure C is illegal because myString might be deleted in the meantime. However I'm unsure about A

Why does this `std::atomic_thread_fence` work

て烟熏妆下的殇ゞ 提交于 2020-07-09 05:21:20
问题 Firstly I want to list some of my undertandings regarding to this, please correct me if I'm wrong. a MFENCE in x86 can ensure a full barrier Sequential-Consistency prevents reordering of STORE-STORE, STORE-LOAD, LOAD-STORE and LOAD-LOAD This is according to Wikipedia. std::memory_order_seq_cst makes no guarantee to prevent STORE-LOAD reorder. This is according to Alex's answer, "Loads May Be Reordered with Earlier Stores to Different Locations"(for x86) and mfence will not always be added.

Why does this `std::atomic_thread_fence` work

孤街浪徒 提交于 2020-07-09 05:21:01
问题 Firstly I want to list some of my undertandings regarding to this, please correct me if I'm wrong. a MFENCE in x86 can ensure a full barrier Sequential-Consistency prevents reordering of STORE-STORE, STORE-LOAD, LOAD-STORE and LOAD-LOAD This is according to Wikipedia. std::memory_order_seq_cst makes no guarantee to prevent STORE-LOAD reorder. This is according to Alex's answer, "Loads May Be Reordered with Earlier Stores to Different Locations"(for x86) and mfence will not always be added.

How is std::atomic<T>::notify_all ordered?

空扰寡人 提交于 2020-07-06 13:55:48
问题 I expect the below program not to hang. If (2) and (3) are observed in reverse order in (1), it may hang due to lost notification: #include <atomic> #include <chrono> #include <thread> int main() { std::atomic<bool> go{ false }; std::thread thd([&go] { go.wait(false, std::memory_order_relaxed); // (1) }); std::this_thread::sleep_for(std::chrono::milliseconds(400)); go.store(true, std::memory_order_relaxed); // (2) go.notify_all(); // (3) thd.join(); return 0; } So the question is what would

atomic_ref when external underlying type is not aligned as requested

╄→尐↘猪︶ㄣ 提交于 2020-06-27 18:35:08
问题 I read on p0019r8 the following: atomic_ref(T& obj); Requires : The referenced object shall be aligned to required_alignment . cppreference interprets this as UB when not aligned: The behavior is undefined if obj is not aligned to required_alignment. So how would you expect from an implementation to handle it? And implementation can check compile-time alignof , but in reality a type might be aligned more that alignof . An implementation can interpret pointer bits and check runtime alignment,

C++11: the difference between memory_order_relaxed and memory_order_consume

我只是一个虾纸丫 提交于 2020-06-24 05:16:49
问题 I am now learning C++11 memory order model and would like to understand the difference between memory_order_relaxed and memory_order_consume . To be specific, I am looking for a simple example where one cannot replace memory_order_consume with memory_order_relaxed . There is an excellent post which elaborates on a simple yet very illustrative example where memory_order_consume can be applied. Below is literal copy-paste. Example: atomic<int*> Guard(nullptr); int Payload = 0; Producer: Payload

How to achieve a StoreLoad barrier in C++11?

谁说胖子不能爱 提交于 2020-06-08 04:52:27
问题 I want to write portable code (Intel, ARM, PowerPC...) which solves a variant of a classic problem: Initially: X=Y=0 Thread A: X=1 if(!Y){ do something } Thread B: Y=1 if(!X){ do something } in which the goal is to avoid a situation in which both threads are doing something . (It's fine if neither thing runs; this isn't a run-exactly-once mechanism.) Please correct me if you see some flaws in my reasoning below. I am aware, that I can achieve the goal with memory_order_seq_cst atomic store s

How do I make memory stores in one thread “promptly” visible in other threads?

谁都会走 提交于 2020-05-28 06:07:01
问题 Suppose I wanted to copy the contents of a device register into a variable that would be read by multiple threads. Is there a good general way of doing this? Here are examples of two possible methods of doing this: #include <atomic> volatile int * const Device_reg_ptr = reinterpret_cast<int *>(0x666); // This variable is read by multiple threads. std::atomic<int> device_reg_copy; // ... // Method 1 const_cast<volatile std::atomic<int> &>(device_reg_copy) .store(*Device_reg_ptr, std::memory

How do I make memory stores in one thread “promptly” visible in other threads?

假如想象 提交于 2020-05-28 06:06:49
问题 Suppose I wanted to copy the contents of a device register into a variable that would be read by multiple threads. Is there a good general way of doing this? Here are examples of two possible methods of doing this: #include <atomic> volatile int * const Device_reg_ptr = reinterpret_cast<int *>(0x666); // This variable is read by multiple threads. std::atomic<int> device_reg_copy; // ... // Method 1 const_cast<volatile std::atomic<int> &>(device_reg_copy) .store(*Device_reg_ptr, std::memory

C++11 on modern Intel: am I crazy or are non-atomic aligned 64-bit load/store actually atomic?

血红的双手。 提交于 2020-05-16 08:04:33
问题 Can I base a mission-critical application on the results of this test, that 100 threads reading a pointer set a billion times by a main thread never see a tear? Any other potential problems doing this besides tearing? Here's a stand-alone demo that compiles with g++ -g tear.cxx -o tear -pthread . #include <atomic> #include <thread> #include <vector> using namespace std; void* pvTearTest; atomic<int> iTears( 0 ); void TearTest( void ) { while (1) { void* pv = (void*) pvTearTest; intptr_t i =