我阅读了关于锁和线程性能计数器的文章,但我仍然不理解其中一些计数器的含义。我特别指的是队列长度和争用率计数器及其每秒钟的对应计数器。MSDN表示第一个计数器显示等待锁的线程数量,第二个计数器显示未成功获取锁的线程数量。我认为如果一个线程正在等待锁,那么这意味着锁没有被获取,但显然我是错的?
计数器运行时会显示什么?
假设我有这个样例程序:
static void Main(string[] args)
{
var t1 = new Thread(RunThread1);
var t2 = new Thread(RunThread2);
t1.Start();
t2.Start();
t1.Join();
t2.Join();
}
static void RunThread1()
{
Thread.Sleep(1000);
// this lock is acquired immediately. What will counters show at this moment?
// probably both will be zero?
lock (m_Lock)
{
Thread.Sleep(10000);
}
}
static void RunThread2()
{
Thread.Sleep(2000);
// this lock has to wait for about 9 seconds. What will counters show?
lock (m_Lock)
{
Thread.Sleep(10000);
}
}
计数器运行时会显示什么?