Wor*_*mbo 5 c# concurrency c#-3.0 c#-4.0
根据语言规范lock(obj) statement;将编译为:
object lockObj = obj; // (the langspec doesn't mention this var, but it wouldn't be safe without it)
Monitor.Enter(lockObj);
try
{
statement;
}
finally
{
Monitor.Exit(lockObj);
}
Run Code Online (Sandbox Code Playgroud)
但是,编译为:
try
{
object lockObj = obj;
bool lockTaken = false;
Monitor.Enter(lockObj, ref lockTaken);
statement;
}
finally
{
if (lockTaken) Monitor.Exit(lockObj);
}
Run Code Online (Sandbox Code Playgroud)
这似乎比必要的要复杂得多.所以问题是,该实施的优势是什么?
一如既往,Eric Lippert已经回答了这个问题:
| 归档时间: |
|
| 查看次数: |
748 次 |
| 最近记录: |