昨天我发布了thisquestion关于如何编写快速自旋锁。感谢CoryNelson,我似乎找到了一种优于我问题中讨论的其他方法的方法。我使用CMPXCHG指令来检查锁是否为0从而释放。CMPXCHG对“BYTE”、WORD和DWORD进行操作。我假设该指令在BYTE上运行得更快。但是我写了一个实现每种数据类型的锁:inlinevoidspin_lock_8(char*lck){__asm{movebx,lck;movelckpointerintoebxxorcl,cl;setCLto0inccl;incrementCLto1pause;spin_loop:xoral,al;setAL
虽然我不确定为什么,但这似乎并不完全正确。建议很好,因为CMPXCHG16B的文档非常少(我没有任何英特尔手册...)templateinlineboolcas(volatiletypes::uint128_t*src,types::uint128_tcmp,types::uint128_twith){/*Description:TheCMPXCHG16Binstructioncomparesthe128-bitvalueintheRDX:RAXandRCX:RBXregisterswitha128-bitmemorylocation.Ifthevaluesareequal,theze
对于我对在Windowsx64上使用原子互锁操作不感兴趣的类型,是否可以哄骗std::atomic输出CMPXCHG16B,或者我是否只需要接受它并手动执行原子操作?我可以让GCC/Clang在Linux上执行此操作,所以我怀疑这只是Microsoft标准库的问题。structByte16{int64_ta,b;};std::atomicatm;Byte16a={1,2};atm.compare_exchange_strong(...);//ThishasalockonWindows,notonLinuxversionofcode 最佳答案
linux内核接口atomic_long_try_cmpxchg_acquire详解1atomic_long_try_cmpxchg_acquire/release1.1atomic_long_try_cmpxchg_acquire1.2atomic_long_try_cmpxchg_release2arch_atomic64_cmpxchg_acquire/release2.1arch_atomic64_cmpxchg_acquire/release定义2.2atomic64_cmpxchg_acquire/release2.3instrument_atomic_read_write2.4a