Which is more efficient, basic mutex lock or atomic integer?

前端 未结 7 1637
误落风尘
误落风尘 2020-12-07 12:57

For something simple like a counter if multiple threads will be increasing the number. I read that mutex locks can decrease efficiency since the threads have to wait. So, to

7条回答
  •  失恋的感觉
    2020-12-07 13:42

    If you have a counter for which atomic operations are supported, it will be more efficient than a mutex.

    Technically, the atomic will lock the memory bus on most platforms. However, there are two ameliorating details:

    • It is impossible to suspend a thread during the memory bus lock, but it is possible to suspend a thread during a mutex lock. This is what lets you get a lock-free guarantee (which doesn't say anything about not locking - it just guarantees that at least one thread makes progress).
    • Mutexes eventually end up being implemented with atomics. Since you need at least one atomic operation to lock a mutex, and one atomic operation to unlock a mutex, it takes at least twice long to do a mutex lock, even in the best of cases.

提交回复
热议问题