Is if almost always required to have thread syncing (i.e. use of mutex, semaphores, critical sections, etc.) when there is cross-thread data accessing, even if it’s not required after going through a requirements analysis?
Is if almost always required to have thread syncing (i.e. use of mutex, semaphores,
Share
I would always recommended going with the simplest, most straightforward synchronization scheme until analysis shows you should do otherwise – this usually means a few large locks versus many fine-grained locks or lockfree.
The issue is that determining if lock-free code is correct is much more difficult than determining if the corresponding code with locks is correct. This creates a large burden on maintainers of your code, and there is a good chance they will get it wrong and introduce bugs. Even if you know that lock-free is safe with how your code is currently used, this could change in the future by people who aren’t as aware.
Secondly, in many cases, the difference in performance between code with locks and lock-free code is neglible – until you know there is an issue with lock contention, you should not consider lock-free. Even if there are contention problems, lock-free is not necessarily the best solution.