简体   繁体   English

lock语句使用什么类型的锁定机制

[英]What type of locking mechanism does lock statement use

Does the c# lock keyword use a 'yielding', 'spin-locking' or hybrid approach to handle contention? c#lock关键字是否使用“让步”,“自旋锁定”或混合方法来处理争用?

So far my searches on the .net lock statement hasn't turned up an answer. 到目前为止,我对.net锁定语句的搜索没有找到答案。 I will post if I do find any more. 如果我找到了,我会发布。 So far all I could find is When should one use a spinlock ... with a nicely worded accepted answer by Mecki. 到目前为止,我所能找到的是什么时候应该使用一个自旋锁...用Mecki接受的措辞很好。

But I am looking for some definitive answer or documentation regarding .net/c# if anyone has one. 但我正在寻找关于.net / c#的一些明确的答案或文件,如果有人的话。

Following code: 以下代码:

lock (_syncRoot)
{
    // Do stuff
}

Is translated by the compiler to: 由编译器翻译为:

Monitor.Enter(_syncRoot)
try
{
    // Do stuff
}
finally
{
    Monitor.Exit(_syncRoot);
}

This is the naive (and old) implementation, actually with .NET 4.0 the implementation is more or less this (see Eric's blog for complete reference): 这是天真的(和旧的)实现,实际上使用.NET 4.0实现或多或少这个(参见Eric的博客以获得完整的参考):

bool locked = false;
try
{
    Monitor.Enter(_syncRoot, ref locked);
}
finally
{
    if (locked)
        Monitor.Exit(_syncRoot);
}

EDITED EDITED

That said the question is how Monitor.Enter() works? 那说问题是Monitor.Enter()如何工作? Well, default Mono implementation uses a semaphore to acquire the lock but Microsoft .NET implementation acts different. 好吧,默认的Mono实现使用信号量来获取锁,但Microsoft .NET实现的行为不同。

I was reading Concurrent Windows Programming (by Joe Duffy) when a paragraph did catch my attention, my first answer said "no, it doesn't use spinning because performance may not be good in general cases". 当一个段落引起我的注意时,我正在阅读Concurrent Windows Programming (由Joe Duffy撰写),我的第一个回答说“不,它不使用旋转,因为在一般情况下性能可能不太好”。 Correct answer is "yes, .NET Monitor uses spinning". 正确答案是“是的,.NET Monitor使用旋转”。 Both .NET Monitor and Windows Critical Sections perform a short spinning before falling back to a true wait on a kernel object. .NET Monitor和Windows Critical Sections都会在回退到内核对象的真正等待之前执行短暂的旋转。 This algorithm is called "two-phase locking protocol" and it's appropriate because context switches and kernel transitions are very expansive, on a multiprocessor machine spinning can avoid both of them. 这种算法被称为“两阶段锁定协议”,它是合适的,因为上下文切换和内核转换是非常广泛的,在多处理器机器上旋转可以避免它们。

Moreover do not forget these are implementation details and can change in any release (or algorithm can be different for different hardwares because of JIT compiler). 此外,不要忘记这些是实现细节,并且可以在任何版本中进行更改(或者由于JIT编译器,算法对于不同的硬件可能会有所不同)。

lock (obj)
{
}

was just syntactic sugar for Monitor . 只是Monitor的语法糖。 Enter in a try...finally. 最后输入 ...

Monitor.Enter(obj);
try
{
}
finally
{
    Monitor.Exit(obj);
}

It is now something a little better (Thanks to Mark and Adriano for keeping me current). 现在好一点 (感谢马克和阿德里亚诺让我保持最新状态)。

bool locked = false;  
try  
{  
    Monitor.Enter(_syncRoot, ref locked);  
}  
finally  
{  
    if (locked)  
        Monitor.Exit(_syncRoot);  
} 

This is Microsoft documentation saying lock() wraps Monitor.Enter/Exit 这是Microsoft文档说lock()包装Monitor.Enter / Exit

"use the C# lock statement, which wraps the Enter and Exit methods in a try…finally block." “使用C#lock语句,它将try和Exit方法包装在try ... finally块中。”

from http://msdn.microsoft.com/en-us/library/de0542zz.aspx 来自http://msdn.microsoft.com/en-us/library/de0542zz.aspx

If you want a spinlock type you can use ReaderWriterLockSlim() 如果你想要一个螺旋锁类型,你可以使用ReaderWriterLockSlim()

http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim.aspx http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM