How is NET 4.0 SpinWait method different to pre-4.0 SpinWait()?

情到浓时终转凉″ 提交于 2019-12-10 10:21:51

问题


MSDN "Thread-Safe Collections .NET Framework 4" states:

"Some of the concurrent collection types use lightweight synchronization mechanisms such as SpinLock, SpinWait, SemaphoreSlim, and CountdownEvent, which are new in the .NET Framework 4"

while MSDN website tells that SpinWaitwas was available as far as .NET 1.1 while another MSDN article starts SpinWaitwas from .NET 4.0

Well, the curiosity is from the comment by Lee Grissom to answer What is the difference between SynchronizedCollection and the other concurrent collections?:

"@Matt, the .NET4 concurrent classes use SpinWait objects to address thread-safety instead of Monitor.Enter/Exit (aka Critical section)?"

as well as first NSDN quote stating that SpinWait is new to .NET 4.0

So, is it new or not?
And if new then how?


回答1:


The struct System.Threading.SpinWait is new to .NET 4.0. The method System.Threading.Thread.SpinWait() exists since .NET 1.0.

Note that System.Threading.SpinWait internally calls System.Threading.Thread.SpinWait(). For further details see "Concurrent Programming On Windows" by Joe Duffy (chapter 14, section "Spin Waiting").




回答2:


The System.Threading.SpinWait structure was introduced into .NET 4. The System.Threading.Thread.SpinWait method has been present since .NET 1.0.

From the docs for the SpinWait structure:

SpinWait is not generally useful for ordinary applications. In most cases, you should use the synchronization classes provided by the .NET Framework, such as Monitor. For most purposes where spin waiting is required, however, the SpinWait type should be preferred over the SpinWait method.

Note the part that says you shouldn't be using either of them in most cases :)



来源:https://stackoverflow.com/questions/15110860/how-is-net-4-0-spinwait-method-different-to-pre-4-0-spinwait

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!