I'm asking specifically in the memory-model sense. http://en.cppreference.com/w/cpp/atomic/memory_order
I'm asking because I want to know if I can use a std::memory_order_consume
in the below:
mLocalMemPtr1 and 2 and mAtomicMemPtr are pointers into a shared buffer.
In a producer thread I'm doing:
for (int x = 0; x < 10; ++x)
{
++mLocalMemPtr1
*mLocalMemPtr1 = x; // <========= A
mAtomicMemPtr.store(mLocalMemPtr1, std::memory_order_release);
}
And in the consumer:
tempMemPtr = mAtomicMemPtr.load(std::memory_order_consume);
while (tempMemPtr != mLocalMemPtr2)
{
++mLocalMemPtr2;
int test = *mLocalMemPtr2; // <======== B
doSomeLongRunningThing(test);
tempMemPtr = mAtomicMemPtr.load(std::memory_order_consume);
}
So does the dependency chain go tempMemPtr -> mLocalMemPtr2 -> test -> doSomeLongRunningThing?
I'm specifically worried that B
may be executed before A
. I know I can use a std::memory_order_acquire
, but I can use consume (which is more lightweight) if the conditional statement causes a memory order dependency.