更新时间:2023-02-12 12:21:16
锁/SyncLock 的等价物是使用 Monitor 类.
The equivelent to a lock / SyncLock would be to use the Monitor class.
在 .NET 1-3.5sp 中,lock(obj) 会:
In .NET 1-3.5sp, lock(obj) does:
Monitor.Enter(obj);
try
{
// Do work
}
finally
{
Monitor.Exit(obj);
}
从 .NET 4 开始,它将是:
As of .NET 4, it will be:
bool taken = false;
try
{
Monitor.Enter(obj, ref taken);
// Do work
}
finally
{
if (taken)
{
Monitor.Exit(obj);
}
}
您可以通过以下方式将其转换为 C++:
You could translate this to C++ by doing:
System::Object^ obj = gcnew System::Object();
Monitor::Enter(obj);
try
{
// Do work
}
finally
{
Monitor::Exit(obj);
}