473,666 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Synchronization Attribute and Monitor.Wait deadlock

I've found the docs to be rather scant on exactly what the second parameter to WaitHandle.Wait One(Int32, Boolean) was
for. The one named exitContext so I started playing with the synchronize attribute to find out how it all works.

I'm puzzled at why this blocks until it times out.

What is supposed to happen is the consumer thread is started and waits for a producer to signal that something is ready.
This behavior is true if reEntrant == true.

What actually happens is that the consumer never releases its context lock on the object so the producer is unable to
enter the object whilst the consumer is waiting.

So what is the difference? Why can't Monitor.Wait release its lock on the context unless reEntrant == true?
class Program
{
public static void Main()
{
Tester test = new Tester();
Thread t1 = new Thread(new ThreadStart(tes t.Consume));
Thread t2 = new Thread(new ThreadStart(tes t.Produce));

t1.Start();

//ensure our consumer gets a lock and waits before starting the producer
Thread.Sleep(10 );
t2.Start();

t1.Join();
t2.Join();

Console.ReadKey (true);
}
}

//note: that reEntrant == false, if it's set to true there is no problem
[Synchronization (Synchronizatio nAttribute.REQU IRES_NEW, false)]
public class Tester : ContextBoundObj ect
{
private object resource_lock = new object();

//this method will not run on the second thread until the consumer times out and the first thread
//leaves the class (and thus the context).
public void Produce()
{
Console.WriteLi ne("Tester begin produce: ThreadId = " + Thread.CurrentT hread.ManagedTh readId);
lock (resource_lock)
{
//just pretend we are doing something then signal the consumer
Thread.Sleep(10 00);
Monitor.Pulse(r esource_lock);
}
Console.WriteLi ne("Tester end produce: ThreadId = " + Thread.CurrentT hread.ManagedTh readId);
}

public void Consume()
{
Console.WriteLi ne("Tester begin consume: ThreadId = " + Thread.CurrentT hread.ManagedTh readId);
lock (resource_lock)
{
//This will block indefinitely without the timeout, but should this not release the context?
if (Monitor.Wait(r esource_lock, 5000, true)) {
Console.WriteLi ne("Tester, Item consumed");
} else {
Console.WriteLi ne("Tester, TIMED OUT");
}
}
Console.WriteLi ne("Tester end consume: ThreadId = " + Thread.CurrentT hread.ManagedTh readId);
}
}
OUTPUT:
Tester begin consume: ThreadId = 4
Tester, TIMED OUT
Tester end consume: ThreadId = 4
Tester begin produce: ThreadId = 5
Tester end produce: ThreadId = 5

EXPECTED OUTPUT: (this behavior is exibited if reEntrant == true)
Tester begin consume: ThreadId = 4
Tester begin produce: ThreadId = 5
Tester end produce: ThreadId = 5
Tester, Item consumed
Tester end consume: ThreadId = 4
Apr 28 '06 #1
0 2467

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

Similar topics

1
4236
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
2928
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,
12
1878
by: Brett Robichaud | last post by:
I need to make access to a reference object threadsafe. My natural instinct was to simply use Monitor.Enter() and Exit(). The problem is that the object behind the reference changes frequently, so my understanding of Monitor indicates this would not protect my object. Example: Bitmap bmp = new Bitmap("x.jpg"); Monitor.Enter(bmp); bmp = new Bitmap("y.jpg"); Monitor.Exit(bmp);
25
2690
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?
13
2539
by: Jonathan Amsterdam | last post by:
I think there's a slight design flaw in the Queue class that makes it hard to avoid nested monitor deadlock. The problem is that the mutex used by the Queue is not easy to change. You can then easily get yourself into the following situation (nested monitor deadlock): Say we have a class that contains a Queue and some other things. The class's internals are protected by a mutex M. Initially the Queue is empty. The only access to the...
12
5261
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...
3
2065
by: Adam Clauss | last post by:
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
2
2487
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...
2
4501
by: =?Utf-8?B?QmVuIFIu?= | last post by:
Hi, I'm new to threading, and am trying to understand monitor.pluse. I get the general idea, but what does this offer that simply using a semaphore does not offer? If you have two threads, couldn't you just immediately have one thread in the ciritical section and have the other try to enter. It will wait until the lock is handed back, and the lock could continue to be passed back and forth this way. The off thread will sleep until it's...
0
8866
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...
0
8781
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8550
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
8639
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...
0
7385
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6192
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
5663
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
4198
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
4366
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.