SpinLock.Enter(Boolean) 方法
定义
采用可靠的方式获取锁,这样,即使在方法调用中发生异常的情况下,都能采用可靠的方式检查 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.
public:
void Enter(bool % lockTaken);
public void Enter (ref bool lockTaken);
member this.Enter : bool -> unit
Public Sub Enter (ByRef lockTaken As Boolean)
参数
- lockTaken
- Boolean
如果已获取锁,则为 true,否则为 false。True if the lock is acquired; otherwise, false. 调用此方法前,必须将 lockTaken 始化为 false。lockTaken must be initialized to false prior to calling this method.
例外
在调用 Enter 之前,lockTaken 参数必须初始化为 false。The lockTaken argument must be initialized to false prior to calling Enter.
线程所有权跟踪已启用,当前线程已获取此锁定。Thread ownership tracking is enabled, and the current thread has already acquired this lock.
注解
SpinLock 是非重入锁,这意味着,如果线程持有锁,则不允许再次进入该锁。SpinLock is a non-reentrant lock, meaning that if a thread holds the lock, it is not allowed to enter the lock again. 如果启用了线程所有权跟踪 (通过) 是否可以使用它 IsThreadOwnerTrackingEnabled ,则当某个线程尝试重新进入它已经持有的锁时,将引发异常。If thread ownership tracking is enabled (whether it's enabled is available through IsThreadOwnerTrackingEnabled), an exception will be thrown when a thread tries to re-enter a lock it already holds. 但是,如果禁用了线程所有权跟踪,尝试输入已持有的锁将导致死锁。However, if thread ownership tracking is disabled, attempting to enter a lock already held will result in deadlock.
如果调用时 Exit 没有首先调用的 Enter 内部状态,则 SpinLock 可能会损坏。If you call Exit without having first called Enter the internal state of the SpinLock can become corrupted.