What is the purpose of the Thread.SpinWait method?

From MSDN its purpose is not quite clear.

Can it be used to simulate an intensive CPU calculation test?

+57
multithreading
Jul 07 '09 at 8:56
source share
4 answers

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.

+97
Jul 07 '09 at 9:08
source share

The goal is to make a β€œcheap” wait if you think that the condition you are expecting will come true very soon. Usually, if you were waiting for something, you let the thread sleep, and the processor / OS switches the context to another thread. Context switches are not particularly cheap, so if you have advanced knowledge of the situation and find it cheaper to wait than switch to context, you will wait.

My advice: if you need to ask, you do not need to use it. (I never wanted this myself.) In fact, this is one of those things that is really useful in very few situations, but most people should be left alone.

+69
Jul 07 '09 at 9:00
source share

As an additional note, Microsoft got rid of the thread manager spinlock mechanism from Windows 7 because it does not scale well for multi-core processors. Check this out :

+14
Jul 07 '09 at 9:44
source share

As far as I know (and I'm glad for the corrections!), The only use of spin expectations is the implementation of a blocking mechanism or cross-thread callback. And none of them should be done manually (usually), since they already exist.

When you have locked a resource and another thread is requesting synchronized access to it, it basically needs to wait for the first thread to finish. This expectation can be accomplished by simply rotating in a loop (or else sleeping + context switching, as John mentioned).

+5
Jul 07 '09 at 9:07
source share



All Articles