語言

SpinLock(Boolean) 建構函式

定義

初始化結構的新實例 SpinLock ,並可選擇追蹤執行緒 ID,以改善除錯。

public:
 SpinLock(bool enableThreadOwnerTracking);
public SpinLock(bool enableThreadOwnerTracking);
new System.Threading.SpinLock : bool -> System.Threading.SpinLock
Public Sub New (enableThreadOwnerTracking As Boolean)

參數

enableThreadOwnerTracking
Boolean

是否要擷取並使用執行緒 ID 來除錯。

範例

以下範例展示了 SpinLock 的使用方式。

// C#
public class MyType
{
    private SpinLock _spinLock = new SpinLock();

    public void DoWork()
    {
        bool lockTaken = false;
        try
        {
           _spinLock.Enter(ref lockTaken);
           // do work here protected by the lock
        }
        finally
        {
            if (lockTaken) _spinLock.Exit();
        }
    }
}
' Visual Basic

Class MyType
   Private _spinLock As New SpinLock()

   Public Sub DoWork()
      Dim lockTaken As Boolean = False
      Try
         _spinLock.Enter(lockTaken)
         ' do work here protected by the lock
      Finally
         If lockTaken Then _spinLock.Exit()
      End Try
   End Sub
End Class

備註

無參數建構器用於 SpinLock 追蹤緒所有權。

適用於

另請參閱