From: Tony Johansson on
Hi!

Here I have some code that could cause deadlock
I have tried to force a deadlock from this program but I can't the only
thing that I have noticed is that sometimes it take a time before the First
and Second is written to the Console screen.
So I have two qestions.
1. Is there perhaps some automatic release when a deadlock has been detected
by the OS or .NET.
I mean if I perhaps get a deadlock when the first and second didn't were
written immediately but the deadlock was release automatically.

2. If I didn't get a deadlock after 50 attempts it seems to be very rare to
get. I mean you could get it some time but you never know when.

using System;
using System.Threading;

class Deadlocker
{
object ResourceA = new object();
object ResourceB = new object();

public void First()
{
lock(ResourceA)
{
lock (ResourceB)
{
Console.WriteLine("First");
}
}
}

public void Second()
{
lock (ResourceB)
{
lock (ResourceA)
{
Console.WriteLine("Second");
}
}
}
}

class Test
{
public static void Main()
{
Deadlocker deallock = new Deadlocker();
ThreadStart firstStart = new ThreadStart(deallock.First);
ThreadStart secondStart = new ThreadStart(deallock.Second);

Thread first = new Thread(firstStart);
Thread second = new Thread(secondStart);

first.Start();
second.Start();

first.Join();
second.Join();

Console.ReadLine();
}
}


From: Peter Duniho on
Tony Johansson wrote:
> Hi!
>
> Here I have some code that could cause deadlock
> I have tried to force a deadlock from this program but I can't the only
> thing that I have noticed is that sometimes it take a time before the First
> and Second is written to the Console screen.
> So I have two qestions.
> 1. Is there perhaps some automatic release when a deadlock has been detected
> by the OS or .NET.
> I mean if I perhaps get a deadlock when the first and second didn't were
> written immediately but the deadlock was release automatically.

No, there is no automatic deadlock recovery in Windows or .NET.

> 2. If I didn't get a deadlock after 50 attempts it seems to be very rare to
> get. I mean you could get it some time but you never know when.


Yes. That's one of the difficult things about multithreaded
programming. Even when the code is wrong, it can work successfully for
thousands or even millions of iterations before you notice a problem.

50 attempts is not nearly sufficient for you to to be likely to see a
problem, especially when each trial happens only once per process
execution, and while statistically unlikely, it is _possible_ to run
code like that for days, weeks, or even months without it breaking.

Hopefully though, if you let it run in a loop for a few million times,
you'll eventually be able to see the deadlock happen.

Pete
From: Mihajlo Cvetanović on
On 5/10/2010 5:07 PM, Tony Johansson wrote:
> 2. If I didn't get a deadlock after 50 attempts it seems to be very rare to
> get. I mean you could get it some time but you never know when.

Put Thread.Sleep(2000) between two locks on both places. This will get
you deadlocked. Probably.