Permalink
Fetching contributors…
Cannot retrieve contributors at this time
34 lines (28 sloc) 2.22 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
How to: Enable Thread-Tracking Mode in SpinLock
03/30/2017
.net
dotnet-standard
article
SpinLock, how to enable thread-tracking
62ee2e68-0bdd-4869-afc9-f0a57a11ae01
8
rpetrusha
ronpet
wpickett

How to: Enable Thread-Tracking Mode in SpinLock

xref:System.Threading.SpinLock?displayProperty=nameWithType is a low-level mutual exclusion lock that you can use for scenarios that have very short wait times. xref:System.Threading.SpinLock is not re-entrant. After a thread enters the lock, it must exit the lock correctly before it can enter again. Typically, any attempt to re-enter the lock would cause deadlock, and deadlocks can be very difficult to debug. As an aid to development, xref:System.Threading.SpinLock?displayProperty=nameWithType supports a thread-tracking mode that causes an exception to be thrown when a thread attempts to re-enter a lock that it already holds. This lets you more easily locate the point at which the lock was not exited correctly. You can turn on thread-tracking mode by using the xref:System.Threading.SpinLock constructor that takes a Boolean input parameter, and passing in an argument of true. After you complete the development and testing phases, turn off thread-tracking mode for better performance.

Example

The following example demonstrates thread-tracking mode. The lines that correctly exit the lock are commented out to simulate a coding error that causes one of the following results:

  • An exception is thrown if the xref:System.Threading.SpinLock was created by using an argument of true (True in Visual Basic).

  • Deadlock if the xref:System.Threading.SpinLock was created by using an argument of false (False in Visual Basic).

[!code-csharpCDS_SpinLock#01] [!code-vbCDS_SpinLock#01]

See Also

SpinLock