Skip to content

Latest commit

 

History

History
29 lines (22 loc) · 2.67 KB

how-to-use-spinlock-for-low-level-synchronization.md

File metadata and controls

29 lines (22 loc) · 2.67 KB
description title ms.date dev_langs helpviewer_keywords ms.assetid
Learn more about: How to: use SpinLock for low-level synchronization
How to: use SpinLock for low-level synchronization
03/30/2017
csharp
vb
SpinLock, how to use
a9ed3e4e-4f29-4207-b730-ed0a51ecbc19

How to: use SpinLock for low-level synchronization

The following example demonstrates how to use a xref:System.Threading.SpinLock. In this example, the critical section performs a minimal amount of work, which makes it a good candidate for a xref:System.Threading.SpinLock. Increasing the work a small amount increases the performance of the xref:System.Threading.SpinLock compared to a standard lock. However, there is a point at which a SpinLock becomes more expensive than a standard lock. You can use the concurrency profiling functionality in the profiling tools to see which type of lock provides better performance in your program. For more information, see Concurrency Visualizer.

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

xref:System.Threading.SpinLock might be useful when a lock on a shared resource is not going to be held for very long. In such cases, on multi-core computers it can be efficient for the blocked thread to spin for a few cycles until the lock is released. By spinning, the thread does not become blocked, which is a CPU-intensive process. xref:System.Threading.SpinLock will stop spinning under certain conditions to prevent starvation of logical processors or priority inversion on systems with Hyper-Threading.

This example uses the xref:System.Collections.Generic.Queue%601?displayProperty=nameWithType class, which requires user synchronization for multi-threaded access. Another option is to use the xref:System.Collections.Concurrent.ConcurrentQueue%601?displayProperty=nameWithType, which does not require any user locks.

Note the use of false in the call to xref:System.Threading.SpinLock.Exit%2A?displayProperty=nameWithType. This provides the best performance. Specify true on IA64 architectures to use the memory fence, which flushes the write buffers to ensure that the lock is now available for other threads to enter.

See also