PC Review


Reply
Thread Tools Rate Thread

Behavior of Monitor.Wait and exception

 
 
Adam Clauss
Guest
Posts: n/a
 
      12th Jul 2006
Documentation states that a call to Wait releases the lock, and will not
return until the lock is reacquired.

How do exceptions figure into this? Are they considered a form of "return"?

Example:

lock (someObj)
{
try
{
Monitor.Wait(someObj);
}
catch (Exception ex)
{
//What is the state of the lock here?
}
}

Will the lock ALWAYS be re-acquired even for exceptions (such as
ThreadInterruptedException)?

Thanks!
--
Adam Clauss



 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      12th Jul 2006
Adam Clauss <(E-Mail Removed)> wrote:
> Documentation states that a call to Wait releases the lock, and will not
> return until the lock is reacquired.
>
> How do exceptions figure into this? Are they considered a form of "return"?
>
> Example:
>
> lock (someObj)
> {
> try
> {
> Monitor.Wait(someObj);
> }
> catch (Exception ex)
> {
> //What is the state of the lock here?
> }
> }
>
> Will the lock ALWAYS be re-acquired even for exceptions (such as
> ThreadInterruptedException)?


In 1.0/1.1, there's a bug where in certain situations the lock isn't
reacquire. In 2.0 this appears to be fixed. I'm not entirely sure what
the behaviour is though - it could lead to deadlock even if the thread
is being aborted, which doesn't sound great.

See http://www.yoda.arachsys.com/csharp/threads/abort.shtml for a
complete example showing the broken behaviour on 1.1 (it works on 2.0).

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Adam Clauss
Guest
Posts: n/a
 
      13th Jul 2006
"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>> Will the lock ALWAYS be re-acquired even for exceptions (such as
>> ThreadInterruptedException)?

>
> In 1.0/1.1, there's a bug where in certain situations the lock isn't
> reacquire. In 2.0 this appears to be fixed. I'm not entirely sure what
> the behaviour is though - it could lead to deadlock even if the thread
> is being aborted, which doesn't sound great.
>
> See http://www.yoda.arachsys.com/csharp/threads/abort.shtml for a
> complete example showing the broken behaviour on 1.1 (it works on 2.0).



Interesting writeup... I wasn't aware of that bug. Although I cannot say I
have ever actually used Interrupt or Abort. I'm not using them in my
current situation either, but rather attempting to account for the
possibility that someone else may use them.

I am in .NET 2.0, so for now, I am going to go for the hope that it DOES
reacquire the lock (I do not see any method to "check to see" if the current
thread owns a lock?)

--
Adam Clauss


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      13th Jul 2006
Adam Clauss wrote:
> > See http://www.yoda.arachsys.com/csharp/threads/abort.shtml for a
> > complete example showing the broken behaviour on 1.1 (it works on 2.0).

>
> Interesting writeup... I wasn't aware of that bug. Although I cannot say I
> have ever actually used Interrupt or Abort. I'm not using them in my
> current situation either, but rather attempting to account for the
> possibility that someone else may use them.
>
> I am in .NET 2.0, so for now, I am going to go for the hope that it DOES
> reacquire the lock (I do not see any method to "check to see" if the current
> thread owns a lock?)


By the looks of it, it does reacquire the lock - I don't know what
happens if another thread has got it and never releases it though. And
no, I don't know of any way of testing whether or not you hold a lock
without any potential side-effects. (Pulsing the lock or waiting on it
will throw an exception if you don't hold the lock, but they could have
side-effects if you do.)

Jon

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dotnet compact 1.0 does not support Monitor.Wait() and Monitor.Pulse() lsheung@gmail.com Microsoft Dot NET Framework 1 7th Feb 2006 08:38 PM
Difference between Monitor.Enter/Monitor.Wait and AutoResetEvent.W =?Utf-8?B?TG9yZW56bw==?= Microsoft Dot NET Framework 1 24th Nov 2005 02:55 PM
RE: Monitor.Exit and Monitor.Wait not documented correctly =?Utf-8?B?YXNhbmZvcmQ=?= Microsoft Dot NET Framework 9 2nd Dec 2004 12:43 AM
Monitor.Wait/Monitor.Pulse ozbear Microsoft Dot NET Framework 2 6th Sep 2004 10:13 AM
Best replacement for wait/notify (Monitor.Wait()/Monitor.Pulse()) on the CompactFramework? Carl Rosenberger Microsoft Dot NET Compact Framework 5 19th Sep 2003 07:31 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:40 AM.