Skip to content

Instantly share code, notes, and snippets.

@luisdeol
Last active November 19, 2017 18:37
Show Gist options
  • Save luisdeol/d9c71bb64e889091dff341704b61f7f7 to your computer and use it in GitHub Desktop.
Save luisdeol/d9c71bb64e889091dff341704b61f7f7 to your computer and use it in GitHub Desktop.
Example of deadlock using lock
namespace manage_multithreading
{
class Program
{
static void Main(string[] args)
{
object _lockSaviorOne = new object();
object _lockSaviorTwo = new object();
Task leTask = Task.Run(() =>
{
lock (_lockSaviorOne)
{
Thread.Sleep(1000);
lock (_lockSaviorTwo)
Console.WriteLine("Hey, One and Two are already locked!");
}
});
Task leSecondTask = Task.Run(() =>
{
lock (_lockSaviorTwo)
lock (_lockSaviorOne)
Console.WriteLine("Yeah, One and Two are locked!");
});
Task.WaitAll(leTask, leSecondTask);
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment