LDC optimized builds causing unexpected behaviour when sharing variable between threads

David Nadlinger code at klickverbot.at
Sat Jun 4 21:33:30 UTC 2022


On 2 Jun 2022, at 7:56, Johan via digitalmars-d-ldc wrote:
> `atomicFence` only works for ordering memory operations on a single 
> thread, not ordering between threads.

`atomicFence` should work across threads as well; it inserts actual 
processor fences as necessary, in addition to the requisite effect on 
compiler optimisations.

However, in practice, code using standalone fences often harder to make 
sense of than that using atomic loads/stores, unless the relevant 
accesses are explicitly documented in enough detail. Just sprinkling 
`atomicFence` over an existing code base to make it thread-safe is 
pretty much guaranteed to fail. Thus, preferring atomic loads/stores is 
very sound advice in practice.

(There are also fences that are effectively for compiler optimisations 
only and usually go by volatile fences or signal fences; perhaps 
that’s what Johan was thinking of.)

  — David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-ldc/attachments/20220604/27b3efe2/attachment.htm>


More information about the digitalmars-d-ldc mailing list