Skip to content

Instantly share code, notes, and snippets.

@in-async
Last active September 20, 2023 08:09
Show Gist options
  • Save in-async/15dc92d104a1d8733fb3e0c1b0b71f17 to your computer and use it in GitHub Desktop.
Save in-async/15dc92d104a1d8733fb3e0c1b0b71f17 to your computer and use it in GitHub Desktop.
`TaskCompletionSource<T>` でデッドロックする例
TaskCompletionSource<object?> tcs = new(
// NOTE: RunContinuationsAsynchronously を付けるとデッドロックしない(非同期に継続される)
// TaskCreationOptions.RunContinuationsAsynchronously
);
ManualResetEventSlim mre = new();
_ = Task.Run(() => {
Console.WriteLine("Start background");
tcs.TrySetResult(null); // 2
mre.Set(); // (4)
Console.WriteLine("End background");
});
await tcs.Task.ConfigureAwait(false); // 1
mre.Wait(); // 3