Comparison of Linux spin lock and mutex efficiency and usage scenario __linux

Source: Internet
Author: User

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).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.