no. It was used as a substitute for very short sleep calls.
When you are doing multi-threaded locking, if the resource you are trying to acquire is already locked, you usually go to bed and wait until it is released. When you do this, you will leave the remaining time that you set as the scheduler to use the processor so that someone else can leave. This is usually normal, especially with long wait times, such as waiting for I / O, other processes may start on the processor while you wait for the disk to spin.
However, sometimes you wait a tiny amount of time. In such cases, you usually give up your remaining time and wait for all the other threads to do their job before moving on ... so you can cheat, not wait, you sit there constantly, showing that "we almost the same way: if the lock is held only for the remainder of the time, this becomes a very effective means of waiting, which is also very effective, since the scheduler does not have to participate in the permutation of all other threads in order to use the time that you lose if you normally about waiting.
Obviously, if you spin every time you want a lock, you will not be very popular, your application will become sluggish and will use a 100% processor, but in very small doses, at the right time, this makes the application more responsive.
If you now think, βwhen should I use it?β, This difficult call is if you have a resource that very often locks and unlocks very quickly, then spinlocking around this instead of waiting is a good idea (and then test the application for performance ), if you try to twist for a short time, and then return to normal expectation, this is also a reasonable way. But in general, you will never need to use it.
gbjbaanb Jul 07 '09 at 9:08 2009-07-07 09:08
source share