This is an automated archive made by the Lemmit Bot.
The original was posted on /r/rust by /u/newpavlov on 2025-05-27 21:54:39+00:00.
compare_exchange_weak
is advertised as:
function is allowed to spuriously fail even when the comparison succeeds, which can result in more efficient code on some platforms
My understanding was that “some platforms” here imply targets with LL/SC instructions which include ARM, PowerPC, and RISC-V. But in practice… there is absolutely no difference between compare_exchange_weak
and compare_exchange
on these targets.
Try changing one to another in this snippet: https://rust.godbolt.org/z/rdsah5G5r The generated assembly stays absolutely the same! I had hopes for RISC-V in this regard, but as you can see in this issue because of the (IMO) bonkers restriction in the ISA spec on retry loops used with LR/SC sequences, compilers (both LLVM and GCC) can not produce a more efficient code for compare_exchange_weak
.
So if you want to optimize your atomic code, you may not bother with using compare_exchange_weak
.