jeudi 24 octobre 2019

Fences Definitions in C++ Staandard [atomics.fences in n4713]

[atomics.fences.2]:

A release fence A synchronizes with an acquire fence B if there exist atomic operations X and Y, both operating on some atomic object M, such that A is sequenced before X, X modifies M, Y is sequenced before B, and Y reads the value written by X or a value written by any side effect in the hypothetical release sequence X would head if it were a release operation.

So a potential sample code is like:

std::atomic<uint32_t> M;

//Thread K
std::atomic_thread_fence(std::memory_order::release);  // release fence A sequenced before
M.store(1, std::memory_order::relaxed); // operation X: operating on atomic object M

//Thread J
M.load(std::memory_order::relaxed); // operation Y: operating on atomic object M -- reading sequenced before
std::atomic_thread_fence(std::memory_order::acquire); // acquire fence B

Questions:

  1. Is the code above correct? If not, what should it look like?

  2. I thought the release fence should be sequenced after operation X because the release fence is trying to make the previous store visible to other threads with respect to the acquire fence (B), and so acquire fence B should be sequenced before operation Y. No?

Aucun commentaire:

Enregistrer un commentaire