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 me, an atomic counter would be the most efficient, but I read that internally it is basically a lock? So I guess I'm confused how either could be more efficient than the other.
相关问题
- How to let a thread communicate with another activ
- Why it isn't advised to call the release() met
- ThreadPoolTaskScheduler behaviour when pool is ful
- Why should we check WIFEXITED after wait in order
- Custom TaskScheduler, SynchronizationContext?
相关文章
- Difference between Thread#run and Thread#wakeup?
- Java/Spring MVC: provide request context to child
- Threading in C# , value types and reference types
- RMI Threads prevent JVM from exiting after main()
- What happens to dynamic allocated memory when call
- Async task does not work properly (doInBackground
- Android, Volley Request, the response is blocking
- parallelizing matrix multiplication through thread
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:
A minimal (standards compliant) mutex implementation requires 2 basic ingredients:
There is no way you can make it any simpler than this because of the 'synchronizes-with' relationship the C++ standard requires.
A minimal (correct) implementation might look like this:
Due to its simplicity (it cannot suspend the thread of execution), it is likely that, under low contention, this implementation outperforms a
std::mutex
. But even then, it is easy to see that each integer increment, protected by this mutex, requires the following operations:atomic
store to release the mutexatomic
compare-and-swap (read-modify-write) to acquire the mutex (possibly multiple times)If you compare that with a standalone
std::atomic<int>
that is incremented with a single (unconditional) read-modify-write (eg.fetch_add
), it is reasonable to expect that an atomic operation (using the same ordering model) will outperform the case whereby a mutex is used.Mutex
is a kernel level semantic which provides mutual exclusion even at theProcess 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.
Atomic operations leverage processor support (compare and swap instructions) and don't use locks at all, whereas locks are more OS-dependent and perform differently on, for example, Win and Linux.
Locks actually suspend thread execution, freeing up cpu resources for other tasks, but incurring in obvious context-switching overhead when stopping/restarting the thread. On the contrary, threads attempting atomic operations don't wait and keep trying until success (so-called busy-waiting), so they don't incur in context-switching overhead, but neither free up cpu resources.
Summing up, in general atomic operations are faster if contention between threads is sufficiently low. You should definitely do benchmarking as there's no other reliable method of knowing what's the lowest overhead between context-switching and busy-waiting.
atomic integer is a user mode object there for it's much more efficient than a mutex which runs in kernel mode. The scope of atomic integer is a single application while the scope of the mutex is for all running software on the machine.
The atomic variable classes in Java are able to take advantage of Compare and swap instructions provided by the processor.
Here's a detailed description of the differences: http://www.ibm.com/developerworks/library/j-jtp11234/