Choice between spin locks and mutually exclusive locks

Source: Internet
Author: User


Spin locks and mutually exclusive locks are two important concepts in multi-thread programming. They can all be used to lock some shared resources to prevent concurrent questions that affect data consistency. But there are indeed differences between them. What are these differences?

1 Theory

Theoretically, when a thread tries to obtain a locked mutually exclusive lock, the operation fails and the thread goes to sleep, so that another thread can be executed immediately. When the thread holding the mutually exclusive lock releases the lock, the thread that enters the sleep state will be awakened. However, when a thread tries to obtain a spin lock but fails, the thread will retry until it is successful. Therefore, the thread will not pass the execution permission to other threads (of course, once the time slice of the current thread times out, the operating system will force the switch to another thread ).

2 Problems

The problem with mutually exclusive locks is that it takes a lot of time for the thread to sleep and wake up the thread. to complete these operations, a lot of CPU instructions are required, so it takes a lot of time. Assuming that only locking mutually exclusive locks takes a very short period of time, it may take more time for the thread to sleep and wake up the thread than the thread actually sleeps, it may even exceed the thread's polling lock waste time on the spin lock (assuming that the spin lock is used ). On the other hand, polling in the spin lock will waste CPU time. If the spin lock is locked for a long time, it may waste a lot of CPU time, at this time, it may be a better choice to make the thread sleep.

3. Solution

Using a spin lock in a single-core system does not work. As long as the spin lock polling is blocking the current CPU, no other thread can be executed. Since no other thread can be executed, then the lock will not be awakened. Yes, we are in a deadlock. In the best case, spin locks only waste CPU time that is useless to the system. On the contrary, assuming that thread a enters sleep using mutually exclusive locks, another thread B will execute immediately. Thread B may release the lock and wake up thread, enable thread a to continue execution.

 

In a multi-core system, assuming that a large number of locks are held for a very short period of time, the time wasted for thread sleep and awakening may greatly reduce the execution performance. On the contrary, assuming that the spin lock is used, the thread will have the opportunity to use the full time slice (which is always blocked for a very short period of time and then executed immediately) for higher throughput.

4. Practice

In most cases, programmers cannot know whether to use mutually exclusive locks or spin locks (for example, because they do not know the number of CPU cores of the target system ), at the same time, the operating system does not know whether the code of a segment has been optimized for a Single-core or multi-core environment. Therefore, most systems do not strictly distinguish the two locks. In fact, most modern operating systems provide mixed mutually exclusive locks and mixed spin locks. So what are the mutual exclusion locks and spin locks?

 

In a multi-core system, mixed mutual exclusion locks act like spin locks at the beginning. That is, if thread a cannot obtain mutually exclusive locks, thread a will not immediately enter the sleep state, because the lock may be released immediately, therefore, the mutually exclusive lock starts to look like a spin lock. After a fixed period of time, thread a cannot obtain the mutually exclusive lock before it enters the sleep state. Assuming that the same program is executed in a single-core system, the mutually exclusive lock will not show the spin lock behavior.

 

A mixed spin lock acts like a normal spin lock at the beginning, but to avoid wasting CPU time, it provides a back-off policy. Generally, the mixed spin lock won't bring the thread into sleep (because you don't want this to happen when you use the spin lock ), however, it can stop a thread (immediately or after a fixed period of time), and then let another thread execute, to increase the idle rate of the spin lock (a pure thread switching is usually more efficient than putting the thread into sleep and then waking it up, at least for the moment ).

5. Summary

If you don't know which one to use, use mutually exclusive locks, because most modern operating systems agree that they spin for a short period of time (this spin was intended in advance ), therefore, mutual exclusion locks are generally a better choice. Sometimes, the use of spin locks improves performance. In some specific cases, you may think that the use of spin locks is better. At this time, you use your own lock object. The lock object is implemented using a spin lock or mutual exclusion lock (this behavior can be changed through configuration). At the beginning, all the mutually exclusive locks are used, then, assume that you think it is better to use the spin lock somewhere, then modify it and then compare it with the explain results. But before the conclusion, you must remember to perform the pilot test in the single-core and multi-core environments.

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.