473,785 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(so meObj);
}
catch (Exception ex)
{
//What is the state of the lock here?
}
}

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

Thanks!
--
Adam Clauss

Jul 12 '06 #1
3 2074
Adam Clauss <ca*****@tamu.e duwrote:
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(so meObj);
}
catch (Exception ex)
{
//What is the state of the lock here?
}
}

Will the lock ALWAYS be re-acquired even for exceptions (such as
ThreadInterrupt edException)?
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.co m>
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.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
>Will the lock ALWAYS be re-acquired even for exceptions (such as
ThreadInterrup tedException)?

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
20741
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 ? Thanks
2
2909
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
4243
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. From the log it looks like the problem happens when 2 threads insert 1 record each in the same table and then try to aquire a NS (Next Key Share) lock on the record inserterd by the other thread. Thanks Rohit
11
2939
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 an event handler? Small code example attached. Thanks,
25
2712
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 T2, T3... try to enter the critical section and are blocked. What is the order that the threads get to enter the critical section?
4
3196
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 interchangeable, or when should I use one over the other? TIA Charles
12
5275
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 block protecting a resource. Thread 2 and 3 also have a SyncLock block protecting the same resource and after executing some code in their blocks they both do a Monitor.Pulse to hand of the locked resource back to thread 1. While thread 1 has...
2
2492
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 because Thread1 waits on Thread2 and vice versa, although I can't quite see how. Disabling Thread3 and leaving the sleep in works fine, processing is nicely throttled. Activating Thread3 and we enter a world of pain. I don't understand what the...
3
3067
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 information I need for tracking who is holding the lock. Here is what I get in the event monitor: ------------------------------------------------------------------------
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10329
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.