LDC optimized builds causing unexpected behaviour when sharing variable between threads
Keivan Shah
keivan.shah at silverleafcaps.com
Wed Jun 1 13:35:45 UTC 2022
On Wednesday, 1 June 2022 at 08:23:18 UTC, Johan wrote:
> This is not a bug.
> The solution with `volatileLoad` works, but I think it
> technically isn't quite correct; documentation says: "They
> should not be used for communication between threads.".
>
> There are many articles online about this kind of conditional
> loop on a variable that is modified by another thread. For
> example:
> https://stackoverflow.com/questions/10091112/using-a-global-variable-to-control-a-while-loop-in-a-separate-thread and https://devblogs.microsoft.com/oldnewthing/20180924-00/?p=99805
>
> -Johan
Hey Johan,
Thanks for the quick response. As you mentioned even I don't feel
the solution with `volatileLoad` is correct and so was looking
for something else. The
[documentation](https://dlang.org/articles/migrate-to-shared.html#shared) stated that
> Global data that is meant to be shared among multiple threads
> should be marked with the shared keyword
So to me it seemed like marking the variable as `shared` should
have made it accessible as well as it takes care of the memory
ordering. That plus the fact that the snippet works in `dmd` but
not `ldc` made me think it maybe was a bug. Anyways, have gotten
a better understanding of the issue now, probably will use
[core.atomic.atomicFence](https://dlang.org/phobos/core_atomic.html#.atomicFence) to ensure the memoryOrdering.
Thanks and Regards,
Keivan Shah.
More information about the digitalmars-d-ldc
mailing list