I’m trying to secure some multi-threaded code and am in the process of adding unit tests to prove I’ve fixed corruption we’d previously been seeing.
In doing so though I’ve come across some unexpected behaviour. Why does the following code succeed when the docs indicate that TryEnter should only return true when the function has achieved an exclusive lock?
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?!
Documentation says
See http://msdn.microsoft.com/en-us/library/de0542zz(v=vs.110)