Maybe there is other interpretation of "Dead Lock" but AFAIK :
A deadlock happens when two threads each wait for a resource held by the other, so neither can proceed.
But I've seen couple of answers here on SO which claims that a long wait ( without waiting for each other) is also a deadlock :
namespace ConsoleApplication7
{
public class Program
{
public static void Main(string[] args)
{
LockableClass lockable = new LockableClass();
new Thread(new ParameterizedThreadStart(BackgroundMethod)).Start(lockable);
Thread.Sleep(500);
Console.Out.WriteLine("calling Reset");
lockable.Reset();
}
private static void BackgroundMethod(Object lockable)
{
lock (lockable)
{
Console.Out.WriteLine("background thread got lock now");
Thread.Sleep(Timeout.Infinite);
}
}
}
public class LockableClass
{
public Int32 Value1 { get; set; }
public Int32 Value2 { get; set; }
public void Reset()
{
Console.Out.WriteLine("attempting to lock on object");
lock (this)
{
Console.Out.WriteLine("main thread got lock now");
Value1 = 0;
Value2 = 0;
}
}
}
}
Pardon me but all I see here is a pure lock that hasn't been released. it is not a situation both threads waiting for another. The Thread here does NOT wait for the main thread to finish.
class ILockMySelf
{
void doThat()
{
lock(this){ ... }
}
}
class WeveGotAProblem
{
ILockMySelf anObjectIShouldntUseToLock;
void doThis()
{
lock(anObjectIShouldntUseToLock)
{
// Following line should run in a separate thread
// for the deadlock to happen
anObjectIShouldntUseToLock.doThat();
}
}
}
Same here. the thread which runs doThis
is NOT waiting for the "separate thread" which will run doThat
Question :
- Are Deadlocks involved here?