Permalink
Fetching contributors…
Cannot retrieve contributors at this time
35 lines (29 sloc) 2.34 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: Use SpinWait to Implement a Two-Phase Wait Operation
03/30/2017
.net
dotnet-standard
article
SpinWait, how to synchronize two-phase wait
b2ac4e4a-051a-4f65-b4b9-f8e103aff195
8
rpetrusha
ronpet
wpickett

How to: Use SpinWait to Implement a Two-Phase Wait Operation

The following example shows how to use a xref:System.Threading.SpinWait?displayProperty=nameWithType object to implement a two-phase wait operation. In the first phase, the synchronization object, a Latch, spins for a few cycles while it checks whether the lock has become available. In the second phase, if the lock becomes available, then the Wait method returns without using the xref:System.Threading.ManualResetEvent?displayProperty=nameWithType to perform its wait; otherwise, Wait performs the wait.

Example

This example shows a very basic implementation of a Latch synchronization primitive. You can use this data structure when wait times are expected to be very short. This example is for demonstration purposes only. If you require latch-type functionality in your program, consider using xref:System.Threading.ManualResetEventSlim?displayProperty=nameWithType.

[!code-csharpCDS_SpinWait#03] [!code-vbCDS_SpinWait#03]

The latch uses the xref:System.Threading.SpinWait object to spin in place only until the next call to SpinOnce causes the xref:System.Threading.SpinWait to yield the time slice of the thread. At that point, the latch causes its own context switch by calling xref:System.Threading.WaitHandle.WaitOne%2A on the xref:System.Threading.ManualResetEvent and passing in the remainder of the time-out value.

The logging output shows how often the Latch was able to increase performance by acquiring the lock without using the xref:System.Threading.ManualResetEvent.

See Also

SpinWait
Threading Objects and Features