ReaderWriterLock.UpgradeToWriterLock Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Meningkatkan kunci pembaca ke kunci penulis.
Overload
| Nama | Deskripsi |
|---|---|
| UpgradeToWriterLock(Int32) |
Meningkatkan kunci pembaca ke kunci penulis, menggunakan Int32 nilai untuk waktu habis. |
| UpgradeToWriterLock(TimeSpan) |
Meningkatkan kunci pembaca ke kunci penulis, menggunakan |
UpgradeToWriterLock(Int32)
- Sumber:
- ReaderWriterLock.cs
- Sumber:
- ReaderWriterLock.cs
- Sumber:
- ReaderWriterLock.cs
- Sumber:
- ReaderWriterLock.cs
- Sumber:
- ReaderWriterLock.cs
Meningkatkan kunci pembaca ke kunci penulis, menggunakan Int32 nilai untuk waktu habis.
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
Parameter
- millisecondsTimeout
- Int32
Waktu habis dalam milidetik.
Mengembalikan
Nilai LockCookie.
- Atribut
Pengecualian
millisecondsTimeout kedaluwarsa sebelum permintaan kunci diberikan.
Contoh
Contoh kode berikut menunjukkan cara meminta kunci pembaca, meningkatkan kunci pembaca ke kunci penulis, dan menurunkan tingkat ke kunci pembaca lagi.
Kode ini adalah bagian dari contoh yang lebih besar yang disediakan untuk ReaderWriterLock kelas .
// 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
Keterangan
Ketika utas UpgradeToWriterLock memanggil kunci pembaca dilepaskan, terlepas dari jumlah kunci, dan utas masuk ke akhir antrean untuk kunci penulis. Dengan demikian, utas lain mungkin menulis ke sumber daya sebelum utas yang meminta peningkatan diberikan kunci penulis.
Important
Pengecualian waktu habis tidak dilemparkan sampai utas yang memanggil UpgradeToWriterLock metode dapat memperoleh kembali kunci pembaca. Jika tidak ada utas lain yang menunggu kunci penulis, ini akan segera terjadi. Namun, jika utas lain diantrekan untuk kunci penulis, utas yang memanggil UpgradeToWriterLock metode tidak dapat memperoleh kembali kunci pembaca sampai semua pembaca saat ini telah melepaskan kunci mereka, dan satu utas telah memperoleh dan melepaskan kunci penulis. Ini benar bahkan jika utas lain yang meminta kunci penulis memintanya setelah utas saat ini yang disebut UpgradeToWriterLock metode .
Untuk memulihkan status kunci, panggil DowngradeFromWriterLock menggunakan yang LockCookie dikembalikan oleh UpgradeToWriterLock. Jangan gunakan ini LockCookie dengan RestoreLock.
Ketika utas tidak memiliki kunci pembaca, jangan gunakan UpgradeToWriterLock. Gunakan AcquireWriterLock sebagai gantinya.
Untuk nilai batas waktu yang valid, lihat ReaderWriterLock.
Lihat juga
-
Managed Threading
Berlaku untuk
UpgradeToWriterLock(TimeSpan)
- Sumber:
- ReaderWriterLock.cs
- Sumber:
- ReaderWriterLock.cs
- Sumber:
- ReaderWriterLock.cs
- Sumber:
- ReaderWriterLock.cs
- Sumber:
- ReaderWriterLock.cs
Meningkatkan kunci pembaca ke kunci penulis, menggunakan TimeSpan nilai untuk waktu habis.
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
Parameter
- timeout
- TimeSpan
TimeSpan menentukan periode waktu habis.
Mengembalikan
Nilai LockCookie.
- Atribut
Pengecualian
timeout kedaluwarsa sebelum permintaan kunci diberikan.
timeout menentukan nilai negatif selain -1 milidetik.
Keterangan
Ketika utas UpgradeToWriterLock memanggil kunci pembaca dilepaskan, terlepas dari jumlah kunci, dan utas masuk ke akhir antrean untuk kunci penulis. Dengan demikian, utas lain mungkin menulis ke sumber daya sebelum utas yang meminta peningkatan diberikan kunci penulis.
Important
Pengecualian waktu habis tidak dilemparkan sampai utas yang memanggil UpgradeToWriterLock metode dapat memperoleh kembali kunci pembaca. Jika tidak ada utas lain yang menunggu kunci penulis, ini akan segera terjadi. Namun, jika utas lain diantrekan untuk kunci penulis, utas yang memanggil UpgradeToWriterLock metode tidak dapat memperoleh kembali kunci pembaca sampai semua pembaca saat ini telah melepaskan kunci mereka, dan satu utas telah memperoleh dan melepaskan kunci penulis. Ini benar bahkan jika utas lain yang meminta kunci penulis memintanya setelah utas saat ini yang disebut UpgradeToWriterLock metode .
Untuk memulihkan status kunci, panggil DowngradeFromWriterLock menggunakan yang LockCookie dikembalikan oleh UpgradeToWriterLock. Jangan gunakan ini LockCookie dengan RestoreLock.
Ketika utas tidak memiliki kunci pembaca, jangan gunakan UpgradeToWriterLock. Gunakan AcquireWriterLock sebagai gantinya.
Untuk nilai batas waktu yang valid, lihat ReaderWriterLock.
Lihat juga
-
Managed Threading