Hi
Why doesn't the following code throw a
SynchronizationLockException? MSDN
says "SynchronizationLockException is thrown if the
current thread does not own the lock for the specified
object".
TIA
Sankar
Imports System.threading
Imports system
Imports microsoft.visualbasic
Module test
Sub Main
Dim t1 As new thread(Addressof foo)
Dim t2 As New thread(addressof goo)
t1.start
thread.sleep(2000)
t2.start
End Sub
sub foo
monitor.enter(Gettype(test))
Console.writeline("sleeping for 5 secs")
thread.sleep(5000)
Console.writeline("after sleeping for 5
secs")
monitor.exit(gettype(test))
end sub
sub goo
try
Console.writeline("trying exit")
monitor.exit(gettype(test))
Console.writeline("after exit")
catch ex As Exception
Console.writeline(ex)
end try
end sub
end module
Jon Skeet [C# MVP] - 24 Oct 2003 04:25 GMT
> Why doesn't the following code throw a
> SynchronizationLockException? MSDN
> says "SynchronizationLockException is thrown if the
> current thread does not own the lock for the specified
> object".
Yes, and by the time you call goo, you no longer own the lock - you've
acquired it at the start of foo, then released it - you then try to
release it again, which causes the exception to be thrown.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
David Browne - 25 Oct 2003 19:44 GMT
> Hi
> Why doesn't the following code throw a
[quoted text clipped - 4 lines]
> TIA
> Sankar
Apparently the documentation is incorrect. No exception is thrown by
Monitor.Exit regardless of whether or not some other thread has entered the
monitor.
David
Jon Skeet [C# MVP] - 26 Oct 2003 00:23 GMT
> Apparently the documentation is incorrect. No exception is thrown by
> Monitor.Exit regardless of whether or not some other thread has entered the
> monitor.
Yikes! (Apologies to the OP for misreading the question earlier, btw.)
That's not so much a documentation problem as a bug in the framework,
IMO. What I haven't tested is whether it means that the lock count for
that monitor is decreased (so another thread could then enter the
monitor before a *real* owner exits it) or not.
Either way, that's really unpleasant :(

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too