What's the purpose of Thread.SpinWait method?

前端 未结 4 1241
耶瑟儿~
耶瑟儿~ 2020-12-12 18:05

From the MSDN is not really clear its purpose.

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

4条回答
  •  忘掉有多难
    2020-12-12 18:33

    The purpose is to do a "cheap" wait if you believe that the condition you're waiting for will come true very, very soon. Normally if you're waiting for something, you let the thread go to sleep and the processor/OS will context switch to another thread. Context switches aren't particularly cheap, so if you have advanced knowledge of the situation and believe it's cheaper to wait than to context switch, you spin wait.

    My advice: if you need to ask, you don't need to use it. (I've never wanted it myself.) Basically it's one of those things which is really useful in a very few situations, but most people should leave well alone.

提交回复
热议问题