What you're seeing is expected behavior. There is nothing special about the actual variable used to pass a reference into Monitor.Enter()
. Changing the reference should not prevent other threads from acquiring an exclusive lock, as the variable has a new value, and that reference is not locked anywhere.
Your issue comes with the Exit
, because the thread calling Exit
does not have an exclusive lock on the reference being passed in. Another thread may well have a lock on it, but the thread you're executing in does not.
This, as you know, is why it's always best to do your locking with a variable whose reference will never change. If your resource's variable might change, use a new reference.
Is this clear enough?