June 05, 2022

On Saturday, 4 June 2022 at 21:33:30 UTC, David Nadlinger wrote:

>

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.

What I meant to say is that the fence only works for ordering memory operations of the thread that contains the fence, as observable by other threads. Indeed you use it to coordinate between threads.
If thread A has a fence somewhere, then that fence orders the memory operations of thread A that thread B will observe. So you can use variables from thread A in thread B, for example: if (value_is_ok_flag) foo(value);. The fence in thread A (between writing value and ok_flag) prevents that ok_flag==true is observed in thread B before the new value value is observed.

Because the OP issue is not a thread synchronization issue, but a re-read optimization (only need to read variable once, instead of upon every loop iteration), the atomic fence is likely not to work. I think the atomicLoad works because it is both volatile and atomic.

cheers,
Johan

June 06, 2022
On 02/06/2022 20:41, Keivan Shah wrote:
> On Thursday, 2 June 2022 at 11:56:07 UTC, Johan wrote:
>> `atomicFence` only works for ordering memory operations on a single thread, not ordering between threads.
>>
>> You can either use a mutex (to coordinate between threads), or need all store/loads to be atomic with `atomic{Store,Load}`.
>> I am surprised that there is no `core.atomic!int` to simplify your life. Perhaps you should make a feature request :)
>>
>> -Johan
> 
> Hey Johan,
> Thanks for the reply, I finally ended up using `atomic{Store,Load}` with some memory ordering after reading about them a bit. Atomic variables (`core.atomic!int`) would have been a great feature, I have no further usecases for them and they probably are a bit more complex for my understanding to make a informed request right now, but will definitely make one when my understanding improves in the future.
> 
> Thanks and regards,
> Keivan Shah.

You can check this:
https://github.com/rymrg/drm/blob/main/atomic.d

It works well enough for my needs at the moment. But it requires more extensive testing to see if it plays nicely with @safe. But for __gshared it should work just fine.
June 15, 2022
On Monday, 6 June 2022 at 08:08:45 UTC, rm wrote:
> You can check this:
> https://github.com/rymrg/drm/blob/main/atomic.d

You should disable postblit or it will be copyable without atomic operation.
1 2
Next ›   Last »