Quantcast
Channel: Is std::mutex enough for making all prior reads-writes happen before following reads-writes all in same thread? - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Is std::mutex enough for making all prior reads-writes happen before following reads-writes all in same thread?

$
0
0

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.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images