21 ms·
What about variables locked with mutexes or semaphores?
by TedDoesntTalk 3y ago
What about variables locked with mutexes or semaphores?
- sweetjuly 3y agolocks and mutexes will perform worse than things like atomic increment.
- bluGill 3y agoMaybe. It depends on the algorithm. If you share a lot of data than a mutex, update it all then unlock is fastest. If you share little data atomics can be faster. This is case be case on both the algorithm and hardware, so nobody can say which is better. Of course not sharing at all is of course best, but often you have no choice in that.
- loeg 3y agoThe significant cost comes from contending on memory. Changing the synchronization primitive doesn't help much. You have to change your algorithm not to contend. This is significant because if you profile your code and find that a mutex is expensive, you should change your algorithm to avoid contention rather than blindly trying to change the code to use atomics instead of mutexes.