Quantcast
Channel: Which is more efficient, basic mutex lock or atomic integer? - Stack Overflow
Viewing all articles
Browse latest Browse all 8

Answer by Sunil Singhal for Which is more efficient, basic mutex lock or atomic integer?

$
0
0

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.


Viewing all articles
Browse latest Browse all 8

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>