The code below demonstrates why it is not guaranteed that 4-byte value being written by another thread is read either as original or final, but it can be read “partially written”:
static constexpr int offset=2;
alignas(64) char vars[64+4-offset];
static volatile unsigned * const p = reinterpret_cast<unsigned *>(&vars[64-offset]);
unsigned getVar()
{
return *p;
}
void loop()
{
while(true)
{
*p = -1;
*p = 0;
}
}
#include <thread>
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <map>
int main()
{
std::thread thread(loop);
std::map<unsigned,int> xs;
for(int i=0;i<10000000;++i)
{
const auto x=getVar();
++xs[x];
}
for(const auto& x : xs)
std::cout << std::setfill('0') << std::setw(8) << std::hex << x.first << ": " << std::dec << x.second << " times\n";
std::exit(0); // exit, killing the thread without abnormal termination via std::terminate
}



