Last active
November 19, 2017 18:37
-
-
Save luisdeol/d9c71bb64e889091dff341704b61f7f7 to your computer and use it in GitHub Desktop.
Example of deadlock using lock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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