core.stdc.stdatomic

claptrap clap at trap.com
Fri Nov 17 16:15:12 UTC 2023


On Friday, 17 November 2023 at 12:41:39 UTC, IGotD- wrote:
> On Friday, 17 November 2023 at 10:25:31 UTC, claptrap wrote:
>>
>> What instructions the compiler inserts around the CAS 
>> instruction are irrelevant, none of what they do can break the 
>> CAS.
>
> It is not irrelevant. You cannot break the CAS itself as it is 
> usually implemented according to the SW ABI of the 
> architecture. However, you must instruct the compiler so that 
> reordering optimizations don't spill over the CAS 
> implementations. If optimizations make instructions spill over 
> to the wrong side of the CAS, then you possibly will end up in 
> a non working algorithm.

You're missing the point.

If your compiler is reordering instructions around a CAS it's 
broken.

If it is doing that then its a problem whether or not you wrapped 
the CAS in a function call or not.

And not only that but the instructions for the function are all 
thread local. The only shared thing the function has is the 
address of the atomic variable in memory. Theres nothing in that 
situation the compiler can reorder that would break multithreaded 
code that would not also break single thraded code.

If your CAS intrinsic or instruction needs a fence, it needs it 
whether inline or wrapped in a function. Wrapping it in a 
function call doesnt somehow cause ordering issues.




More information about the Digitalmars-d mailing list