共用方式為


ReaderWriterLockSlim.CurrentReadCount 屬性

定義

取得已進入讀取模式鎖定狀態的唯一執行緒總數。

public:
 property int CurrentReadCount { int get(); };
public int CurrentReadCount { get; }
member this.CurrentReadCount : int
Public ReadOnly Property CurrentReadCount As Integer

屬性值

Int32

已進入讀取模式鎖定狀態的唯一執行緒數目。

範例

下列範例示範如何在讀取模式中的執行緒數目超過臨界值時,使用 CurrentReadCount 屬性來產生事件記錄檔專案。

using (ReaderWriterLockSlim rwLock = new ReaderWriterLockSlim()) {
Using rwLock As New ReaderWriterLockSlim()
if (!EventLog.SourceExists("MySource"))
{
    EventLog.CreateEventSource("MySource", "MyPerformanceLog");
}
EventLog performanceLog = new EventLog();
performanceLog.Source = "MySource";
If Not EventLog.SourceExists("MySource") Then
    EventLog.CreateEventSource("MySource", "MyPerformanceLog")
End If
Dim performanceLog As New EventLog()
performanceLog.Source = "MySource"
int readCt = rwLock.CurrentReadCount;
if (readCt > READ_THRESHOLD)
{
    performanceLog.WriteEntry(String.Format(
        "{0} reader threads; exceeds recommended maximum.", readCt));
}
Dim readCt As Integer = rwLock.CurrentReadCount
If readCt > READ_THRESHOLD Then
    performanceLog.WriteEntry(String.Format( _
        "{0} reader threads; exceeds recommended maximum.", readCt))
End If

備註

即使鎖定允許遞迴且執行緒已進入讀取模式多次,執行緒也只會計算一次。

僅針對偵錯、分析及記錄目的使用這個屬性,而不是控制演算法的行為。 結果可以在計算結果後立即變更。 因此,根據這個屬性做出決策並不安全。

適用於