Jon*_*age 2 .net multithreading unit-testing c++-cli monitor
我正在尝试保护一些多线程代码,并且我正在添加单元测试以证明我已经修复了我们之前看到的腐败.
尽管我遇到了一些意想不到的行为.当文档表明TryEnter只有在函数实现独占锁定时才返回true时,为什么以下代码成功?
const int msToWaitForLock = 1;
Object^ syncObj = gcnew Object();
bool gotLock = Threading::Monitor::TryEnter(syncObj, msToWaitForLock); // <-- this succeeds as expected
bool gotSecondLock = Threading::Monitor::TryEnter(syncObj, msToWaitForLock); // <-- but why the heck does this succeed?!
Run Code Online (Sandbox Code Playgroud)