473,396 Members | 1,938 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,396 software developers and data experts.

Joining Threads vs. Monitors

Hello,

Hope this is the right group for asking this. Didn't see a threading
specific newsgroup here.

I currently expire unexpected dead-locks within some trivial C# code.
Now I wonder if it is expected behaviour, that monitors do no wake up,
when the thread previously owning the monitor class class Join on the
thread waiting for the monitor? Simplified the code I have looks like this:

class DeadLock
{
object mutex = new object();
Thread thread = null;

void Start()
{
thread = new Thread(Worker);
thread.Start();
}

void Cancel()
{
lock(mutex) {
/* ... */
}

thread.Join();
}

void Worker()
{
/* ... */

lock(mutex) {
/* ... */
}
}
}

When calling Cancel before Worker reaches its critical section I
observe, the following:

- Cancel acquires the monitor and enters its section
- shortly later Worker tries to get the same monitor and blocks
- Cancel releases the monitor and calls waits for Worker to terminate
- Worker never gets be monitor, if I do not manually kill Cancel

I really do not understand, what causes this dead-lock in .NET 2.0.

Thanks,
Mathias Hasselmann
Nov 17 '06 #1
4 1339
Mathias Hasselmann wrote:
I currently expire unexpected dead-locks within some trivial C# code.
Now I wonder if it is expected behaviour, that monitors do no wake up,
when the thread previously owning the monitor class class Join on the
thread waiting for the monitor? Simplified the code I have looks like this:
Lock and Join should not affect each other.
class DeadLock
{
object mutex = new object();
Thread thread = null;

void Start()
{
thread = new Thread(Worker);
thread.Start();
}

void Cancel()
{
lock(mutex) { /* ... */ }
thread.Join();
}

void Worker()
{
/* ... */
lock(mutex) { /* ... */ }
}
}

When calling Cancel before Worker reaches its critical section I
observe, the following:

- Cancel acquires the monitor and enters its section
- shortly later Worker tries to get the same monitor and blocks
- Cancel releases the monitor and calls waits for Worker to terminate
- Worker never gets be monitor, if I do not manually kill Cancel

I really do not understand, what causes this dead-lock in .NET 2.0.
As shown here, this code should work. My guess would be that whatever
Cancel is doing while it has the mutex locked is causing Worker to
hang before it gets to the lock statement.

Fwiw, technically this isn't deadlock: that's when one thread locks A
then B, and another locks B then A. The first thread has A locked, but
can't lock B because the other thread has it locked; the second thread
has B locked, but can't lock A because the first thread has it locked

--

www.midnightbeach.com/.net
..NET, from the outside to the inside.
Nov 17 '06 #2
On Fri, 17 Nov 2006, Jon Shemitz wrote:
>
Mathias Hasselmann wrote:
>I currently expire unexpected dead-locks within some trivial C# code.
Now I wonder if it is expected behaviour, that monitors do no wake up,
when the thread previously owning the monitor class class Join on the
thread waiting for the monitor? Simplified the code I have looks like this:

Lock and Join should not affect each other.
Hmm. Hoped someone would tell me "You are trying something completely
stupid. Do/read this and that. Then it will work". Well, appears like I
have to continue reducing the code for getting a compilable minimal
example of my problem on monday. Hopefully it's really just me doing a
stupid mistake.

Ciao
Mathias
Nov 17 '06 #3
Ok, problem found, left out an important detail of my Start() method.
Where I've written:

void Start()
{
thread = new Thread(Worker);
thread.Start();
}

I really had something like:

void Start()
{
lock(mutex) {
Cancel();
thread = new Thread(Worker);
thread.Start();
}
}
Moving Cancel() out of the synchronized section resolves the lock, which
in fact really was a dead-lock - just didn't find the other monitor that
quickly. Just have to figure out for myself now, if that move doesn't
introduce a race-condition...

To find such issues quicker next time I wonder if .NET provides
utilities like ThreadMXBean#findMonitorDeadlockedThreads()[1], which are
quite helpful for finding monitor dead-locks?

Ciao,
Mathias

1:
http://java.sun.com/j2se/1.5.0/docs/...lockedThreads()
Nov 20 '06 #4
Mathias Hasselmann <ma****************@mdc.berlin.dewrote:

<snip>
To find such issues quicker next time I wonder if .NET provides
utilities like ThreadMXBean#findMonitorDeadlockedThreads()[1], which are
quite helpful for finding monitor dead-locks?
Not that I'm aware of, but I have an OrderedLock class you might like
to look at:
http://www.pobox.com/~skeet/csharp/m...e/locking.html
It wouldn't have helped in this particular case, but it might in
others. (And actually you could have hit Break in the debugger and seen
which thread owned the lock.)

--
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
Nov 20 '06 #5

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

Similar topics

2
by: Gregory Bond | last post by:
I've had a solid hunt through the (2.3) documentation but it seems silent on this issue. I have an problem that would naturally run as 2 threads: One monitors a bunch of asyncrhonous external...
6
by: Simon Harvey | last post by:
Hi everyone, I need to make a service that monitors a directory for changes in the files contained within it. I have two questions: 1. I'm going to be using a FileSystemWatcher object to do...
5
by: KevinGPO | last post by:
Two quick queries: 1. I have programmed a little network performance monitor. It monitors a set of remote hosts over a network. It gets the CPU statistics and all gets collected at one of the...
9
by: Eric Sabine | last post by:
Can someone give me a practical example of why I would join threads? I am assuming that you would typically join a background thread with the UI thread and not a background to a background, but...
22
by: Jeff Louie | last post by:
Well I wonder if my old brain can handle threading. Dose this code look reasonable. Regards, Jeff using System; using System.Diagnostics; using System.IO; using System.Threading;
0
by: Lloyd Zusman | last post by:
I have a python-2.5 program running under linux in which I spawn a number of threads. The main thread does nothing while these subsidiary threads are running, and after they all complete, the main...
4
by: chelfter | last post by:
I need to synchronize all the threads to add all the numbers. Threads are created, but not started immediately. When the threads do start, they must start simultaneously, without preferential...
0
by: Qui Sum | last post by:
I write ip/port scanner so users using search on our LAN know if the particular ftp is online or not. The class I write has the following structure: public class IPScanner { public...
12
by: Curious | last post by:
I'm working on a multi-threading project and I've got an issue about shared memory used by multiple threads at the same time. I have two separate threads that have different callback methods....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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...
0
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,...

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.