Does following function write to x[100] always before y[100] is written?
void fenceTest(float * x, float * y /* this could be x too*/)
{
x[100]=3.14f; // maybe atomic write
x[200]=3.14f;
myMutex.lock();
myMutex.unlock();
y[100]=2.72f; // maybe atomic write too
y[200]=2.72f;
}
or mutex needs to be unlocked after end point always even for this single-thread scenario or do we have to use atomic_thread_fence?
void fenceTest(float * x, float * y)
{
x[100]=3.14f; // maybe atomic too
x[200]=3.14f;
std::atomic_thread_fence(std::memory_order_relaxed);
y[100]=2.72f; // maybe atomic write too
y[200]=2.72f;
}
My intention is to tell both CPU and compiler that they are not allowed to reorder any load/store around the sync point so that all x-array operations are complete before any y-array operations begin. I need to separate blocks of reads/writes so that an algorithm such as Kahan-Summation is not broken by compiler's or CPU's reordering.
For a single-threaded application none of this matters, your program will see the reads and writes in the order you perform them, even if they don't actually happen in that order. This stuff only matters when the data is shared across multiple threads and cores.