ReaderWriterLock.UpgradeToWriterLock メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
リーダー ロックをライター ロックにアップグレードします。
オーバーロード
| 名前 | 説明 |
|---|---|
| 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 メソッドを呼び出したスレッドは、現在のすべてのリーダーがロックを解放し、1 つのスレッドがライター ロックを取得して解放するまで、リーダー ロックを再取得できません。 これは、現在のスレッドが UpgradeToWriterLock メソッドを呼び出した後に、ライター ロックを要求した他のスレッドが要求した場合でも当てはまります。
ロック状態を復元するには、DowngradeFromWriterLockによって返されたLockCookieを使用してUpgradeToWriterLockを呼び出します。
LockCookieでは、このRestoreLockを使用しないでください。
スレッドにリーダー ロックがない場合は、 UpgradeToWriterLockを使用しないでください。
AcquireWriterLock を代わりに使用します。
有効なタイムアウト値については、 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 メソッドを呼び出したスレッドは、現在のすべてのリーダーがロックを解放し、1 つのスレッドがライター ロックを取得して解放するまで、リーダー ロックを再取得できません。 これは、現在のスレッドが UpgradeToWriterLock メソッドを呼び出した後に、ライター ロックを要求した他のスレッドが要求した場合でも当てはまります。
ロック状態を復元するには、DowngradeFromWriterLockによって返されたLockCookieを使用してUpgradeToWriterLockを呼び出します。
LockCookieでは、このRestoreLockを使用しないでください。
スレッドにリーダー ロックがない場合は、 UpgradeToWriterLockを使用しないでください。
AcquireWriterLock を代わりに使用します。
有効なタイムアウト値については、 ReaderWriterLockを参照してください。