我正在尝试保护一些多线程代码,并且正在添加单元测试以证明我已经修复了我们之前看到的损坏。
这样做虽然我遇到了一些意想不到的行为。当文档指示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?!