最近我在阅读 .NET 的实现 Hashtable并遇到了一段我不明白的代码。部分代码为:
int num3 = 0;
int num4;
do
{
num4 = this.version;
bucket = bucketArray[index];
if (++num3 % 8 == 0)
Thread.Sleep(1);
}
while (this.isWriterInProgress || num4 != this.version);
整个代码在 System.Collections.Hashtable
(mscorlib Version=4.0.0.0) 的 public virtual object this[object key]
中。
问题是:
Thread.Sleep(1)
在那里的原因是什么?
最佳答案
Sleep(1) 是 Windows 中有记录的让出处理器并允许其他线程运行的方法。您可以在带有注释的引用源中找到此代码:
// Our memory model guarantee if we pick up the change in bucket from another processor,
// we will see the 'isWriterProgress' flag to be true or 'version' is changed in the reader.
//
int spinCount = 0;
do {
// this is violate read, following memory accesses can not be moved ahead of it.
currentversion = version;
b = lbuckets[bucketNumber];
// The contention between reader and writer shouldn't happen frequently.
// But just in case this will burn CPU, yield the control of CPU if we spinned a few times.
// 8 is just a random number I pick.
if( (++spinCount) % 8 == 0 ) {
Thread.Sleep(1); // 1 means we are yeilding control to all threads, including low-priority ones.
}
} while ( isWriterInProgress || (currentversion != version) );
isWriterInProgress 变量是一个可变 bool 值。作者遇到了一些麻烦,英文“violate read”是“volatile read”。基本思想是尽量避免屈服,线程上下文切换非常昂贵,希望作者能尽快完成。如果这没有成功,那么明确让步以避免燃烧 cpu。这在今天可能是用 Spinlock 写的,但 Hashtable 已经很老了。关于内存模型的假设也是如此。
关于c# - 为什么 .NET 内部哈希表中有一个 Thread.Sleep(1)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20006542/