3 ms·
This is one of the reasons using things like boost's boost::shared_ptr (which uses atomics for ref counting) can have a huge time overhead in multithreaded code
by berkut 12y ago
This is one of the reasons using things like boost's boost::shared_ptr (which uses atomics for ref counting) can have a huge time overhead in multithreaded code.
Often people think atomics are much faster than mutexes for simply checking or incrementing single count/bool values or pointer swapping (and they can be), but they can also be just as slow, depending on the use-case and situation.
In certain situations, they can even be slower! as using them in spinlocks doesn't allow the thread to yield, wasting CPU time doing nothing.
- ryao 12y agomutexes and spinlocks use atomic operations to function. If you can use an atomic operation to replace either of them, your program will be quicker. It will be even quicker if you can use per-cpu or per-thread variables to reduce synchronization overhead.