From 7efaf510849425dd295a7933a6ea9a1306f3ea3c Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Sun, 1 Nov 2026 13:22:35 +0100 Subject: [PATCH] Mutex in C#: move sample to .NET 10, add async pitfall demo and ownership tests --- .../MutexClassInCSharp.Tests.csproj | 10 +++--- .../MutexOwnershipTests.cs | 34 +++++++++++++++++++ .../MutexClassInCSharp/MutexAsyncPitfall.cs | 26 ++++++++++++++ .../MutexClassInCSharp.csproj | 2 +- .../MutexClassInCSharp/Program.cs | 8 ++++- 5 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 csharp-advanced-topics/MutexClassInCSharp/MutexClassInCSharp.Tests/MutexOwnershipTests.cs create mode 100644 csharp-advanced-topics/MutexClassInCSharp/MutexClassInCSharp/MutexAsyncPitfall.cs diff --git a/csharp-advanced-topics/MutexClassInCSharp/MutexClassInCSharp.Tests/MutexClassInCSharp.Tests.csproj b/csharp-advanced-topics/MutexClassInCSharp/MutexClassInCSharp.Tests/MutexClassInCSharp.Tests.csproj index cbdcb8863f..8dd8f2532e 100644 --- a/csharp-advanced-topics/MutexClassInCSharp/MutexClassInCSharp.Tests/MutexClassInCSharp.Tests.csproj +++ b/csharp-advanced-topics/MutexClassInCSharp/MutexClassInCSharp.Tests/MutexClassInCSharp.Tests.csproj @@ -1,7 +1,7 @@ - net7.0 + net10.0 enable enable @@ -10,13 +10,13 @@ - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/csharp-advanced-topics/MutexClassInCSharp/MutexClassInCSharp.Tests/MutexOwnershipTests.cs b/csharp-advanced-topics/MutexClassInCSharp/MutexClassInCSharp.Tests/MutexOwnershipTests.cs new file mode 100644 index 0000000000..d291cf11f0 --- /dev/null +++ b/csharp-advanced-topics/MutexClassInCSharp/MutexClassInCSharp.Tests/MutexOwnershipTests.cs @@ -0,0 +1,34 @@ +namespace MutexClassInCSharp.Tests; + +public sealed class MutexOwnershipTests +{ + [Fact] + public void WhenOwnerThreadExitsWithoutRelease_ThenNextWaiterGetsAbandonedMutexExceptionAndOwnsMutex() + { + using var mutex = new Mutex(); + + var owner = new Thread(() => mutex.WaitOne()); + owner.Start(); + owner.Join(); + + Assert.Throws(() => mutex.WaitOne()); + + mutex.ReleaseMutex(); + } + + [Fact] + public void WhenAnotherThreadReleasesMutex_ThenApplicationExceptionIsThrown() + { + using var mutex = new Mutex(); + mutex.WaitOne(); + + Exception? releaseError = null; + var other = new Thread(() => releaseError = Record.Exception(mutex.ReleaseMutex)); + other.Start(); + other.Join(); + + Assert.IsType(releaseError); + + mutex.ReleaseMutex(); + } +} diff --git a/csharp-advanced-topics/MutexClassInCSharp/MutexClassInCSharp/MutexAsyncPitfall.cs b/csharp-advanced-topics/MutexClassInCSharp/MutexClassInCSharp/MutexAsyncPitfall.cs new file mode 100644 index 0000000000..7ebe7f1d73 --- /dev/null +++ b/csharp-advanced-topics/MutexClassInCSharp/MutexClassInCSharp/MutexAsyncPitfall.cs @@ -0,0 +1,26 @@ +namespace MutexClassInCSharp; + +public static class MutexAsyncPitfall +{ + public static async Task ReleaseAfterAwait() + { + using var mutex = new Mutex(); + var threadBefore = Environment.CurrentManagedThreadId; + + mutex.WaitOne(); + await Task.Delay(50); + var threadAfter = Environment.CurrentManagedThreadId; + + try + { + mutex.ReleaseMutex(); + + return $"Released on the same thread ({threadBefore})."; + } + catch (ApplicationException ex) + { + return $"{ex.GetType().Name} on thread {threadAfter} " + + $"(acquired on thread {threadBefore}): {ex.Message}"; + } + } +} diff --git a/csharp-advanced-topics/MutexClassInCSharp/MutexClassInCSharp/MutexClassInCSharp.csproj b/csharp-advanced-topics/MutexClassInCSharp/MutexClassInCSharp/MutexClassInCSharp.csproj index d439800007..7f1dc2eb2f 100644 --- a/csharp-advanced-topics/MutexClassInCSharp/MutexClassInCSharp/MutexClassInCSharp.csproj +++ b/csharp-advanced-topics/MutexClassInCSharp/MutexClassInCSharp/MutexClassInCSharp.csproj @@ -2,7 +2,7 @@ Exe - net7.0 + net10.0 enable enable diff --git a/csharp-advanced-topics/MutexClassInCSharp/MutexClassInCSharp/Program.cs b/csharp-advanced-topics/MutexClassInCSharp/MutexClassInCSharp/Program.cs index 68b30b1503..f0aec7f469 100644 --- a/csharp-advanced-topics/MutexClassInCSharp/MutexClassInCSharp/Program.cs +++ b/csharp-advanced-topics/MutexClassInCSharp/MutexClassInCSharp/Program.cs @@ -2,8 +2,14 @@ public static class Program { - private static void Main(string[] args) + private static async Task Main(string[] args) { + if (args.FirstOrDefault() == "--async-pitfall") + { + Console.WriteLine(await MutexAsyncPitfall.ReleaseAfterAwait()); + return; + } + var fileName = args.FirstOrDefault() ?? "numbers.txt"; WriteNumbers(fileName); }