C++11 Multi threading memory ordering: Relaxed vs Release and Consume
Hi,
I've a question regarding this example
Code:
#include <thread>
#include <atomic>
#include <cassert>
#include <string>
std::atomic<std::string*> ptr;
int data;
void producer()
{
std::string* p = new std::string("Hello");
data = 42;
ptr.store(p, std::memory_order_release);
}
void consumer()
{
std::string* p2;
while (!(p2 = ptr.load(std::memory_order_consume)))
;
assert(*p2 == "Hello"); // never fires: *p2 carries dependency from ptr
assert(data == 42); // may or may not fire: data does not carry dependency from ptr
}
int main()
{
std::thread t1(producer);
std::thread t2(consumer);
t1.join(); t2.join();
}
given here: std::memory_order - cppreference.com
(Yes, I also read that "no known production compilers track dependency chains: consume operations are lifted to acquire operations.")
I'm wondering whether a std::memory_order_relaxed ordering for the load and store would yield the same... The first assertion can only fire if *p2 != "Hello", which can only happen if the load failed. So the only scenario I can think of is if the storing is "character-wise atomic", i.e. a result could be a partial store of "Hello". If someone can elucidate this to me, I'd be grateful.