mercredi 18 décembre 2019

std::memory_order_acquire fence necessary on x86?

Given x86 has a strong memory model, is std::memory_order_acquire fence (not operation) necessary?

For example, if I have this code:

uint32_t read(const uint64_t offset) {
   // m_head_memory_location is char* pointing to the beginning of a mmap-ed named shared memory segment
   // a different process on different core will write to it.
   return *(uint32_t*)m_head_memory_location + offset;
}
....
int main() {
     uint32_t val = 0;
     while (0 != (val = shm.read(some location)));
     .... // use val
}

Do I really need std::atomic_thread_fence(std::memory_order_acquire) before the return statement?

I feel it is not necessary because the goal of the code above is trying to read the first 4 bytes from m_head_memory_location + offset, and so any memory operations after fences being reordered doesn't affect the outcome.

Or there is some side effect making the acquire fence necessary?

Is there any case that an acquire fence (not operation) is necessary on x86?

Any input is welcome.

Thanks!

Aucun commentaire:

Enregistrer un commentaire