diff --git a/ADIS_Csharp/Sync-Ueb02-Latch/ISynch.cs b/ADIS_Csharp/Sync-Ueb02-Latch/ISynch.cs index b5bd34a..c1ba23c 100644 --- a/ADIS_Csharp/Sync-Ueb02-Latch/ISynch.cs +++ b/ADIS_Csharp/Sync-Ueb02-Latch/ISynch.cs @@ -8,6 +8,8 @@ namespace Sync_Ueb02_Latch { void Acquire(); void Release(); + + uint GetAmountOfAquires(); } } diff --git a/ADIS_Csharp/Sync-Ueb02-Latch/Latch.cs b/ADIS_Csharp/Sync-Ueb02-Latch/Latch.cs index f9b8913..e449cd4 100644 --- a/ADIS_Csharp/Sync-Ueb02-Latch/Latch.cs +++ b/ADIS_Csharp/Sync-Ueb02-Latch/Latch.cs @@ -6,12 +6,30 @@ using System.Threading; namespace Sync_Ueb02_Latch { class Latch : ISynch - { private ManualResetEvent waitHandle = new(false); + { + private ManualResetEvent waitHandle = new(false); + private static uint countAquired = 0; + private static object syncLock = new(); public void Acquire() { + lock (syncLock) + { + countAquired++; + } this.waitHandle.WaitOne(); } + + public uint GetAmountOfAquires() + { + uint readCount = 0; + lock(syncLock) + { + readCount = countAquired; + } + return readCount; + } + public void Release() { this.waitHandle.Set(); diff --git a/ADIS_Csharp/Sync-Ueb02-Latch/Turf.cs b/ADIS_Csharp/Sync-Ueb02-Latch/Turf.cs index 7062270..25da511 100644 --- a/ADIS_Csharp/Sync-Ueb02-Latch/Turf.cs +++ b/ADIS_Csharp/Sync-Ueb02-Latch/Turf.cs @@ -14,6 +14,11 @@ namespace Sync_Ueb02_Latch { new Thread(new RaceHorse(i, startBox).Run).Start(); } + // wait until all horses are ready + while(startBox.GetAmountOfAquires() < 5) + { + Thread.Sleep(20); + } Console.WriteLine("Start..."); startBox.Release(); }