473,385 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Behavior of Monitor.Wait and exception

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

Jul 12 '06 #1
3 2051
Adam Clauss <ca*****@tamu.eduwrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 12 '06 #2
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
>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
Jul 13 '06 #3
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

Jul 13 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Vinay C | last post by:
Hi, Can anyone clear me that, when should we use go for mutex, and in which situation should we opt for monitor, lock, semaphone and other objects, in a multithreaded application for synchronization...
2
by: DT | last post by:
Hi All, I understand the Thread.Sleep method is a very effcient wait state. Can the same be said for the Monitor.Wait method? Many Thanks DT
1
by: Rohit Raghuwanshi | last post by:
Hello all, we are running a delphi application with DB2 V8.01 which is causing deadlocks when rows are being inserted into a table. Attaching the Event Monitor Log (DEADLOCKS WITH DETAILS) here....
11
by: Michi Henning | last post by:
Hi, I'm calling Monitor.Wait() from a console event handler. It's not working -- the call to Wait() immediately causes the process to exit. Is it impossible for some reason to call Wait() from...
25
by: vooose | last post by:
Suppose execution of a particular thread T1 hits Monitor.Enter(obj); //critical section and blocks at the first line. (ie someone else is in the critical section) Now suppose more threads...
4
by: Charles Law | last post by:
I've been using monitors a bit lately (some of you may have heard ;-) ) and then up pop Manual and AutoResetEvents , and they look for all the world like the same thing. Are they...
12
by: Perecli Manole | last post by:
I am having some strange thread synchronization problems that require me to better understand the intricacies of Monitor.Wait/Pulse. I have 3 threads. Thread 1 does a Monitor.Wait in a SyncLock...
2
by: DeveloperX | last post by:
Hi, Below is a hacked up version of an example I found on MSDN. My only change was to add a third thread and some Sleeps. That's where my question(s) comes in. In the original it seems to work...
3
by: shorti | last post by:
Hello all, I am running with DB2 UDB V8 on AIX. I am trying to track down what appears to be a lock issue. I turned EVENT MONITOR on with all the options. It doesnt give me the detailed...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.