System.Threading.SpinWait 是一种轻型同步类型,可在低级别方案中使用,以避免内核事件所需的昂贵上下文切换和内核转换。 在多核计算机上,当预期资源不会被长时间占用时,让等待线程在用户模式下自旋几十个或几百个周期,然后重试获取资源,这可能更有效。 如果资源在旋转后可用,便节省了几千个周期。 如果资源仍不可用,那么也只花了几个周期,仍可以进入基于内核的等待。 这种旋转后等待的组合有时称为两阶段等待操作。
SpinWait 旨在与包装内核事件的 .NET 类型结合使用,例如 ManualResetEvent。 SpinWait 还可以单独用于一个程序中的基本旋转功能。
SpinWait 不仅仅是一个空循环。 它经过精心实施,为常规情况提供正确的旋转行为,如果旋转时间足够长(大约内核转换所需的时间长度),它本身将启动上下文切换。 例如,在单核计算机上,SpinWait 会立即生成线程的时间片,因为旋转会阻止所有线程取得进展。 即使在多核计算机上,SpinWait 也会生成时间片,以防等待线程阻止优先级较高的线程或垃圾回收器。 因此,如果在两阶段等待操作中使用 SpinWait,我们建议在 SpinWait 自己启动上下文切换之前调用内核等待。 SpinWait 提供属性 NextSpinWillYield ,可在每次调用 SpinOnce之前检查该属性。 当属性返回 true
时,启动您自己的 Wait 操作。 有关示例,请参阅 如何使用 SpinWait 实现 Two-Phase 等待操作。
如果不想执行两阶段等待操作,只是想一直旋转到某条件为 true,可以启用 SpinWait 执行上下文切换,让它成为 Windows 操作系统环境中的合法成员。 以下基本示例展示了无锁堆栈中的SpinWait。 如果需要高性能线程安全堆栈,请考虑使用 System.Collections.Concurrent.ConcurrentStack<T>。
public class LockFreeStack<T>
{
private volatile Node m_head;
private class Node { public Node Next; public T Value; }
public void Push(T item)
{
var spin = new SpinWait();
Node node = new Node { Value = item }, head;
while (true)
{
head = m_head;
node.Next = head;
if (Interlocked.CompareExchange(ref m_head, node, head) == head) break;
spin.SpinOnce();
}
}
public bool TryPop(out T result)
{
result = default(T);
var spin = new SpinWait();
Node head;
while (true)
{
head = m_head;
if (head == null) return false;
if (Interlocked.CompareExchange(ref m_head, head.Next, head) == head)
{
result = head.Value;
return true;
}
spin.SpinOnce();
}
}
}
Imports System.Threading
Module SpinWaitDemo
Public Class LockFreeStack(Of T)
Private m_head As Node
Private Class Node
Public [Next] As Node
Public Value As T
End Class
Public Sub Push(ByVal item As T)
Dim spin As New SpinWait()
Dim head As Node, node As New Node With {.Value = item}
While True
Thread.MemoryBarrier()
head = m_head
node.Next = head
If Interlocked.CompareExchange(m_head, node, head) Is head Then Exit While
spin.SpinOnce()
End While
End Sub
Public Function TryPop(ByRef result As T) As Boolean
result = CType(Nothing, T)
Dim spin As New SpinWait()
Dim head As Node
While True
Thread.MemoryBarrier()
head = m_head
If head Is Nothing Then Return False
If Interlocked.CompareExchange(m_head, head.Next, head) Is head Then
result = head.Value
Return True
End If
spin.SpinOnce()
End While
End Function
End Class
End Module