What's the purpose of Thread.SpinWait method?

前端 未结 4 1244
耶瑟儿~
耶瑟儿~ 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:37

    As far as I'm concerned (and I'm happy for corrections!), the only use of spin waits is when implementing a locking or inter-thread-callback mechanism. And neither should be done manually (usually), since they already exist.

    When you've locked a resource and another thread requests synchronized access to it, it basically has to wait for the first thread to finish using it. This waiting can be done by simply spinning in a loop (or otherwise sleeping + context switching as mentioned by Jon).

提交回复
热议问题