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

前端 未结 7 1640
误落风尘
误落风尘 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

    Mutex is a kernel level semantic which provides mutual exclusion even at the Process level. Note that it can be helpful in extending mutual exclusion across process boundaries and not just within a process (for threads). It is costlier.

    Atomic Counter, AtomicInteger for e.g., is based on CAS, and usually try attempting to do operation until succeed. Basically, in this case, threads race or compete to increment\decrement the value atomically. Here, you may see good CPU cycles being used by a thread trying to operate on a current value.

    Since you want to maintain the counter, AtomicInteger\AtomicLong will be the best for your use case.

提交回复
热议问题