C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » C# Syntax »

Simulation of Dead Lock in Threads


Posted Date: 20 Mar 2007    Resource Type: Code Snippets    Category: C# Syntax
Author: Prajnan DasMember Level: Gold    
Rating: 1 out of 5Points: 10



Dead Lock is a situation in which there is a cyclic dependency between a pair of locks and two Threads are waiting for each other to have the lock they need released by their counterpart. Dead Lock can be simulated by having nested lock statements in the target methods of the participating Threads in such a way that the Threads try to acquire the lock in opposite order. Presented here is a class called DeadLockSimulator which uses two objects lock1 and lock2 as locks. There are two methods Method1 and Method2. The driver method SimulateDeadLock spawns two threads with the Method1 and Method2 as targets respectively. At the start, thread1 sleeps for a greater amount of time than thread2 so that thread2 acquires the lock2 first. Now thread1 is waiting for lock2 to be released by thread2. But thread2 is also waiting for lock1 to be released by thread1. None of the Threads can proceed further.

The DeadLockSimulator class:

namespace ClassLibrary
{
public class DeadLockSimulator
{
object lock1 = new object();
object lock2 = new object();

public void Method1()
{
Thread.Sleep(20);
lock (lock1)
{
Console.WriteLine("Method1 acquired lock1");
lock (lock2)
{
Console.WriteLine("Method1 acquired lock2");
}
Console.WriteLine("Method1 exiting!");
}
}

public void Method2()
{
Thread.Sleep(10);
lock (lock2)
{
Console.WriteLine("Method2 acquired lock2");
lock (lock1)
{
Console.WriteLine("Method2 acquired lock1");
}
Console.WriteLine("Method2 exiting!");
}
}

public void SimulateDeadLock()
{
Thread thread1 = new Thread(new ThreadStart(Method1));
Thread thread2 = new Thread(new ThreadStart(Method2));
thread1.Start();
thread2.Start();
thread1.Join();
thread2.Join();
}
}
}

If you run the above class you should see output like this:

'Method2 acquired lock2
Method1 acquired lock1'

and the two Threads can not exit from the their target methods.



Responses

Author: Tushar    21 May 2007Member Level: Bronze   Points : 0
I am surprised that this dude here does not give credit to the original CodeProject author
http://www.codeproject.com/aspnet/ThreeTierCodeGenerator.asp

Grow up boy!!


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Making Parameterized Threads in C#
Previous Resource: Complex Arithmetic using Anonymous Method’s closure
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use