mscorlib(4.0.0.0) API with additions
System.Threading.SpinLock Struct Reference

Provides a mutual exclusion lock primitive where a thread trying to acquire the lock waits in a loop repeatedly checking until the lock becomes available. More...

Public Member Functions

 SpinLock (bool enableThreadOwnerTracking)
 Initializes a new instance of the T:System.Threading.SpinLock structure with the option to track thread IDs to improve debugging. More...
 
void Enter (ref bool lockTaken)
 Acquires the lock in a reliable manner, such that even if an exception occurs within the method call, lockTaken can be examined reliably to determine whether the lock was acquired. More...
 
void TryEnter (ref bool lockTaken)
 Attempts to acquire the lock in a reliable manner, such that even if an exception occurs within the method call, lockTaken can be examined reliably to determine whether the lock was acquired. More...
 
void TryEnter (TimeSpan timeout, ref bool lockTaken)
 Attempts to acquire the lock in a reliable manner, such that even if an exception occurs within the method call, lockTaken can be examined reliably to determine whether the lock was acquired. More...
 
void TryEnter (int millisecondsTimeout, ref bool lockTaken)
 Attempts to acquire the lock in a reliable manner, such that even if an exception occurs within the method call, lockTaken can be examined reliably to determine whether the lock was acquired. More...
 
void Exit ()
 Releases the lock. More...
 
void Exit (bool useMemoryBarrier)
 Releases the lock. More...
 

Properties

bool IsHeld [get]
 Gets whether the lock is currently held by any thread. More...
 
bool IsHeldByCurrentThread [get]
 Gets whether the lock is held by the current thread. More...
 
bool IsThreadOwnerTrackingEnabled [get]
 Gets whether thread ownership tracking is enabled for this instance. More...
 

Detailed Description

Provides a mutual exclusion lock primitive where a thread trying to acquire the lock waits in a loop repeatedly checking until the lock becomes available.

Definition at line 14 of file SpinLock.cs.

Constructor & Destructor Documentation

◆ SpinLock()

System.Threading.SpinLock.SpinLock ( bool  enableThreadOwnerTracking)

Initializes a new instance of the T:System.Threading.SpinLock structure with the option to track thread IDs to improve debugging.

Parameters
enableThreadOwnerTrackingWhether to capture and use thread IDs for debugging purposes.

Definition at line 128 of file SpinLock.cs.

Member Function Documentation

◆ Enter()

void System.Threading.SpinLock.Enter ( ref bool  lockTaken)

Acquires the lock in a reliable manner, such that even if an exception occurs within the method call, lockTaken can be examined reliably to determine whether the lock was acquired.

Parameters
lockTakenTrue if the lock is acquired; otherwise, false. lockTaken must be initialized to false prior to calling this method.
Exceptions
T:System.ArgumentExceptionThe lockTaken argument must be initialized to false prior to calling Enter.
T:System.Threading.LockRecursionExceptionThread ownership tracking is enabled, and the current thread has already acquired this lock.

Definition at line 142 of file SpinLock.cs.

◆ Exit() [1/2]

void System.Threading.SpinLock.Exit ( )

Releases the lock.

Exceptions
T:System.Threading.SynchronizationLockExceptionThread ownership tracking is enabled, and the current thread is not the owner of this lock.

Definition at line 362 of file SpinLock.cs.

◆ Exit() [2/2]

void System.Threading.SpinLock.Exit ( bool  useMemoryBarrier)

Releases the lock.

Parameters
useMemoryBarrierA Boolean value that indicates whether a memory fence should be issued in order to immediately publish the exit operation to other threads.
Exceptions
T:System.Threading.SynchronizationLockExceptionThread ownership tracking is enabled, and the current thread is not the owner of this lock.

Definition at line 380 of file SpinLock.cs.

◆ TryEnter() [1/3]

void System.Threading.SpinLock.TryEnter ( ref bool  lockTaken)

Attempts to acquire the lock in a reliable manner, such that even if an exception occurs within the method call, lockTaken can be examined reliably to determine whether the lock was acquired.

Parameters
lockTakenTrue if the lock is acquired; otherwise, false. lockTaken must be initialized to false prior to calling this method.
Exceptions
T:System.ArgumentExceptionThe lockTaken argument must be initialized to false prior to calling TryEnter.
T:System.Threading.LockRecursionExceptionThread ownership tracking is enabled, and the current thread has already acquired this lock.

Definition at line 157 of file SpinLock.cs.

◆ TryEnter() [2/3]

void System.Threading.SpinLock.TryEnter ( TimeSpan  timeout,
ref bool  lockTaken 
)

Attempts to acquire the lock in a reliable manner, such that even if an exception occurs within the method call, lockTaken can be examined reliably to determine whether the lock was acquired.

Parameters
timeoutA T:System.TimeSpan that represents the number of milliseconds to wait, or a T:System.TimeSpan that represents -1 milliseconds to wait indefinitely.
lockTakenTrue if the lock is acquired; otherwise, false. lockTaken must be initialized to false prior to calling this method.
Exceptions
T:System.ArgumentOutOfRangeExceptiontimeout is a negative number other than -1 milliseconds, which represents an infinite time-out -or- timeout is greater than F:System.Int32.MaxValue milliseconds.
T:System.ArgumentExceptionThe lockTaken argument must be initialized to false prior to calling TryEnter.
T:System.Threading.LockRecursionExceptionThread ownership tracking is enabled, and the current thread has already acquired this lock.

Definition at line 170 of file SpinLock.cs.

◆ TryEnter() [3/3]

void System.Threading.SpinLock.TryEnter ( int  millisecondsTimeout,
ref bool  lockTaken 
)

Attempts to acquire the lock in a reliable manner, such that even if an exception occurs within the method call, lockTaken can be examined reliably to determine whether the lock was acquired.

Parameters
millisecondsTimeoutThe number of milliseconds to wait, or F:System.Threading.Timeout.Infinite (-1) to wait indefinitely.
lockTakenTrue if the lock is acquired; otherwise, false. lockTaken must be initialized to false prior to calling this method.
Exceptions
T:System.ArgumentOutOfRangeExceptionmillisecondsTimeout is a negative number other than -1, which represents an infinite time-out.
T:System.ArgumentExceptionThe lockTaken argument must be initialized to false prior to calling TryEnter.
T:System.Threading.LockRecursionExceptionThread ownership tracking is enabled, and the current thread has already acquired this lock.

Definition at line 188 of file SpinLock.cs.

Property Documentation

◆ IsHeld

bool System.Threading.SpinLock.IsHeld
get

Gets whether the lock is currently held by any thread.

Returns
true if the lock is currently held by any thread; otherwise false.

Definition at line 81 of file SpinLock.cs.

◆ IsHeldByCurrentThread

bool System.Threading.SpinLock.IsHeldByCurrentThread
get

Gets whether the lock is held by the current thread.

Returns
true if the lock is held by the current thread; otherwise false.
Exceptions
T:System.InvalidOperationExceptionThread ownership tracking is disabled.

Definition at line 99 of file SpinLock.cs.

◆ IsThreadOwnerTrackingEnabled

bool System.Threading.SpinLock.IsThreadOwnerTrackingEnabled
get

Gets whether thread ownership tracking is enabled for this instance.

Returns
true if thread ownership tracking is enabled for this instance; otherwise false.

Definition at line 116 of file SpinLock.cs.


The documentation for this struct was generated from the following file: