Skip to content

Instantly share code, notes, and snippets.

@kevingosse
Last active June 25, 2019 03:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevingosse/773b7657dd15fd43e01ddf2e38aeedda to your computer and use it in GitHub Desktop.
Save kevingosse/773b7657dd15fd43e01ddf2e38aeedda to your computer and use it in GitHub Desktop.
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