Fastest inline-assembly spinlock

后端 未结 5 1397
梦如初夏
梦如初夏 2020-12-01 05:24

I\'m writing a multithreaded application in c++, where performance is critical. I need to use a lot of locking while copying small structures between threads, for this I hav

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-01 05:57

    Although there is already an accepted answer, there are a few things that where missed that could be used to improve all the answers, taken from this Intel article, all above fast lock implementation:

    1. Spin on a volatile read, not an atomic instruction, this avoids unneeded bus locking, especially on highly contended locks.
    2. Use back-off for highly contested locks
    3. Inline the lock, preferably with intrinsics for compilers where inline asm is detrimental (basically MSVC).

提交回复
热议问题