ReaderWriterLock.UpgradeToWriterLock 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將讀取鎖升級為寫入鎖定。
多載
| 名稱 | Description |
|---|---|
| UpgradeToWriterLock(Int32) |
將讀取鎖定升級為寫入鎖定,並使用 Int32 逾時值。 |
| UpgradeToWriterLock(TimeSpan) |
將讀取鎖定升級為寫入鎖定,並使用 |
UpgradeToWriterLock(Int32)
將讀取鎖定升級為寫入鎖定,並使用 Int32 逾時值。
public:
System::Threading::LockCookie UpgradeToWriterLock(int millisecondsTimeout);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public System.Threading.LockCookie UpgradeToWriterLock(int millisecondsTimeout);
public System.Threading.LockCookie UpgradeToWriterLock(int millisecondsTimeout);
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
member this.UpgradeToWriterLock : int -> System.Threading.LockCookie
member this.UpgradeToWriterLock : int -> System.Threading.LockCookie
Public Function UpgradeToWriterLock (millisecondsTimeout As Integer) As LockCookie
參數
- millisecondsTimeout
- Int32
時間只需毫秒。
傳回
LockCookie 值。
- 屬性
例外狀況
millisecondsTimeout 在鎖申請獲准前就已過期。
範例
以下程式碼範例說明如何請求讀取鎖、將讀取鎖定升級為寫入鎖定,以及再次降級為讀取鎖定。
此程式碼是該類別更大 ReaderWriterLock 範例的一部分。
// The complete code is located in the ReaderWriterLock class topic.
using System;
using System.Threading;
public class Example
{
static ReaderWriterLock rwl = new ReaderWriterLock();
// Define the shared resource protected by the ReaderWriterLock.
static int resource = 0;
' The complete code is located in the ReaderWriterLock class topic.
Imports System.Threading
Public Module Example
Private rwl As New ReaderWriterLock()
' Define the shared resource protected by the ReaderWriterLock.
Private resource As Integer = 0
// Requests a reader lock, upgrades the reader lock to the writer
// lock, and downgrades it to a reader lock again.
static void UpgradeDowngrade(Random rnd, int timeOut)
{
try {
rwl.AcquireReaderLock(timeOut);
try {
// It's safe for this thread to read from the shared resource.
Display("reads resource value " + resource);
Interlocked.Increment(ref reads);
// To write to the resource, either release the reader lock and
// request the writer lock, or upgrade the reader lock. Upgrading
// the reader lock puts the thread in the write queue, behind any
// other threads that might be waiting for the writer lock.
try {
LockCookie lc = rwl.UpgradeToWriterLock(timeOut);
try {
// It's safe for this thread to read or write from the shared resource.
resource = rnd.Next(500);
Display("writes resource value " + resource);
Interlocked.Increment(ref writes);
}
finally {
// Ensure that the lock is released.
rwl.DowngradeFromWriterLock(ref lc);
}
}
catch (ApplicationException) {
// The upgrade request timed out.
Interlocked.Increment(ref writerTimeouts);
}
// If the lock was downgraded, it's still safe to read from the resource.
Display("reads resource value " + resource);
Interlocked.Increment(ref reads);
}
finally {
// Ensure that the lock is released.
rwl.ReleaseReaderLock();
}
}
catch (ApplicationException) {
// The reader lock request timed out.
Interlocked.Increment(ref readerTimeouts);
}
}
' Requests a reader lock, upgrades the reader lock to the writer
' lock, and downgrades it to a reader lock again.
Sub UpgradeDowngrade(rnd As Random, timeOut As Integer)
Try
rwl.AcquireReaderLock(timeOut)
Try
' It's safe for this thread to read from the shared resource.
Display("reads resource value " & resource)
Interlocked.Increment(reads)
' To write to the resource, either release the reader lock and
' request the writer lock, or upgrade the reader lock. Upgrading
' the reader lock puts the thread in the write queue, behind any
' other threads that might be waiting for the writer lock.
Try
Dim lc As LockCookie = rwl.UpgradeToWriterLock(timeOut)
Try
' It's safe for this thread to read or write from the shared resource.
resource = rnd.Next(500)
Display("writes resource value " & resource)
Interlocked.Increment(writes)
Finally
' Ensure that the lock is released.
rwl.DowngradeFromWriterLock(lc)
End Try
Catch ex As ApplicationException
' The upgrade request timed out.
Interlocked.Increment(writerTimeouts)
End Try
' If the lock was downgraded, it's still safe to read from the resource.
Display("reads resource value " & resource)
Interlocked.Increment(reads)
Finally
' Ensure that the lock is released.
rwl.ReleaseReaderLock()
End Try
Catch ex As ApplicationException
' The reader lock request timed out.
Interlocked.Increment(readerTimeouts)
End Try
End Sub
}
End Module
備註
當執行緒呼叫 UpgradeToWriterLock 時,無論鎖數如何,都會釋放讀取鎖,執行緒會進入寫入鎖的隊列末端。 因此,其他執行緒可能會在請求升級的執行緒獲得寫入鎖定之前,先寫入該資源。
Important
逾時例外直到呼叫 UpgradeToWriterLock 該方法的執行緒能重新取得讀取鎖時才會拋出。 如果沒有其他執行緒等待寫入鎖定,這個問題會立即發生。 然而,若另一執行緒被排隊執行寫入鎖,呼叫該 UpgradeToWriterLock 方法的執行緒必須等所有現有讀取器釋放鎖,且有一個執行緒取得並釋放寫入鎖後,才能重新取得讀取鎖。 即使另一個請求寫入鎖的執行緒是在當前執行緒呼叫該 UpgradeToWriterLock 方法之後才請求該鎖,這點也成立。
要恢復鎖定狀態,請 DowngradeFromWriterLock 使用 LockCookie return by UpgradeToWriterLock呼叫。 請勿將此 LockCookie 用於 RestoreLock。
當執行緒沒有讀取鎖定時,請勿使用 UpgradeToWriterLock。 請改用 AcquireWriterLock。
關於有效的逾時值,請參見 ReaderWriterLock。
另請參閱
- 管理的執行緒
- ReaderWriterLock
適用於
UpgradeToWriterLock(TimeSpan)
將讀取鎖定升級為寫入鎖定,並使用 TimeSpan 逾時值。
public:
System::Threading::LockCookie UpgradeToWriterLock(TimeSpan timeout);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public System.Threading.LockCookie UpgradeToWriterLock(TimeSpan timeout);
public System.Threading.LockCookie UpgradeToWriterLock(TimeSpan timeout);
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
member this.UpgradeToWriterLock : TimeSpan -> System.Threading.LockCookie
member this.UpgradeToWriterLock : TimeSpan -> System.Threading.LockCookie
Public Function UpgradeToWriterLock (timeout As TimeSpan) As LockCookie
參數
- timeout
- TimeSpan
那是 TimeSpan 指定暫停期間的。
傳回
LockCookie 值。
- 屬性
例外狀況
timeout 在鎖申請獲准前就已過期。
timeout 指定除 -1 毫秒外的負值。
備註
當執行緒呼叫 UpgradeToWriterLock 時,無論鎖數如何,都會釋放讀取鎖,執行緒會進入寫入鎖的隊列末端。 因此,其他執行緒可能會在請求升級的執行緒獲得寫入鎖定之前,先寫入該資源。
Important
逾時例外直到呼叫 UpgradeToWriterLock 該方法的執行緒能重新取得讀取鎖時才會拋出。 如果沒有其他執行緒等待寫入鎖定,這個問題會立即發生。 然而,若另一執行緒被排隊執行寫入鎖,呼叫該 UpgradeToWriterLock 方法的執行緒必須等所有現有讀取器釋放鎖,且有一個執行緒取得並釋放寫入鎖後,才能重新取得讀取鎖。 即使另一個請求寫入鎖的執行緒是在當前執行緒呼叫該 UpgradeToWriterLock 方法之後才請求該鎖,這點也成立。
要恢復鎖定狀態,請 DowngradeFromWriterLock 使用 LockCookie return by UpgradeToWriterLock呼叫。 請勿將此 LockCookie 用於 RestoreLock。
當執行緒沒有讀取鎖定時,請勿使用 UpgradeToWriterLock。 請改用 AcquireWriterLock。
關於有效的逾時值,請參見 ReaderWriterLock。
另請參閱
- 管理的執行緒
- ReaderWriterLock