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

注解

即使锁允许递归,线程也多次进入读取模式,线程也会计数一次。

仅对调试、分析和日志记录目的使用此属性,而不控制算法的行为。 结果可以在计算结果后立即更改。 因此,基于此属性做出决策是不安全的。

适用于