Last active
June 25, 2019 03:52
-
-
Save kevingosse/773b7657dd15fd43e01ddf2e38aeedda to your computer and use it in GitHub Desktop.
This file contains 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
using System; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace ConsoleApp1 | |
{ | |
class Program | |
{ | |
private static ManualResetEventSlim _mutex = new ManualResetEventSlim(); | |
private static Task _sharedTask; | |
static void Main(string[] args) | |
{ | |
var tcs = new TaskCompletionSource<bool>(); | |
_sharedTask = tcs.Task; | |
Task.Run(Thread1); | |
Thread.Sleep(2000); // Wait for the task to start | |
Task.Run(Thread2); | |
Thread.Sleep(2000); // Wait for the task to start | |
Console.WriteLine("Completing the shared task"); | |
tcs.SetResult(true); | |
Console.WriteLine("Will never get there"); | |
} | |
private static async Task Thread1() | |
{ | |
await _sharedTask; | |
_mutex.Wait(); | |
Console.WriteLine("Will never get there"); | |
} | |
private static async Task Thread2() | |
{ | |
await _sharedTask; | |
_mutex.Set(); | |
Console.WriteLine("Will never get there"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment