For which (if any?) STORE_ORDER
& LOAD_ORDER
does C++11 guarantee that this code runs in finite time?
std::atomic<bool> a{false};
std::thread t{[&]{
while(!a.load(LOAD_ORDER));
}};
a.store(true, STORE_ORDER);
t.join();
I see two issues with this:
Memory order
It seems to me that with release
& aquire
, the compiler and cpu are allowed to reorder my join
(assuming it behaves like a load) before the store
, which would of course break this.
Even with memory_order_seq_cst
, I'm not sure if such a reordering is prohibited because I don't know if join()
actually does any loads or stores.
Visibility
If I understood this question about memory_order_relaxed
correctly, it is not guaranteed that a store with memory_order_relaxed
becomes visible to other threads in a finite amount of time. Is there such a guarantee for any of the other orderings?
I understand that std::atomic
is about atomicity and memory ordering, not about visibility. But I am not aware of any other tools in c++11 that could help me here. Would I need to use a platform-specific tool to get a correctness guarantee here and if yes, which one?
To take this one step further – if I have finiteness, it would be nice to also have some promise about speed. I don't think the C++ standard makes any such promises. But is there any compiler or x86-specific way to get a promise that the store will become visible to the other thread quickly?
In summary: I'm looking for a way to swiftly stop a worker thread that is actually guaranteed to have this property. Ideally this would be platform-independent. But if we can't have that, does it at least exist for x86?