Article See also:
http://www.parallellabs.com/2010/01/31/pthreads-programming-spin-lock-vs-mutex-performance-analysis/
Summarize
(1) The mutex is suitable for the scene with very frequent lock operation, and has better adaptability. Although it will cost more than spin lock (mainly context switching), it can be adapted to the actual development of complex application scenarios and provide greater flexibility to ensure a certain performance.
(2) The Lock/unlock performance of Spin lock is better (less CPU instruction is spent), but it only adapts to scenarios where the critical section runs for a short time. In real software development, it is not a good idea to use spin lock unless the programmer is aware of the lock-operation behavior of its own program (usually a multithreaded program has countless operations on the lock, if the failed lock operation (contended lock requests) Too many words will waste a lot of time to wait for the empty.
(3) A more insurance approach might be to use a Mutex first (conservatively), and then if there is further demand for performance, you can try tuning with spin lock. After all, our programs are not as demanding as the Linux kernel (Linux kernel most commonly used lock operations are spin lock and RW lock).