Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / October 2003

Tip: Looking for answers? Try searching our database.

Monitor.Exit SynchronizationLockException

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sankar Nemani - 24 Oct 2003 01:31 GMT
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


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.