I need to perform some writes in the main thread and call another thread strictly after that to work with that written data. This is just a simple example:
std::atomic<int> x(0);
int z = 0;
int y = 0;
// ...
// ...
// ...
z = 12;
y = 15;
x.store(80, std::memory_order_release);
std::thread thr = std::thread( // Should be called strictly after x.store()
[&]
{
if (x.load(std::memory_order_acquire) < 100)
{
// do stuff
}
});
The above code guarantees that thr
, if called after x.store()
, will always observe any writes performed in the main thread before x.store()
, including non-atomic writes, that's fine. But is it guaranteed that thr
creation will not be moved before x.store()
, or should I use something like condition variable to notify thr
about x
being ready to be read and checked against the condition?