improved horserace

main
Jonas Arnold 4 years ago
parent a6dad1c019
commit eb82f8af58
  1. 2
      ADIS_Csharp/Sync-Ueb02-Latch/ISynch.cs
  2. 20
      ADIS_Csharp/Sync-Ueb02-Latch/Latch.cs
  3. 5
      ADIS_Csharp/Sync-Ueb02-Latch/Turf.cs

@ -8,6 +8,8 @@ namespace Sync_Ueb02_Latch
{ {
void Acquire(); void Acquire();
void Release(); void Release();
uint GetAmountOfAquires();
} }
} }

@ -6,12 +6,30 @@ using System.Threading;
namespace Sync_Ueb02_Latch namespace Sync_Ueb02_Latch
{ {
class Latch : ISynch 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() public void Acquire()
{ {
lock (syncLock)
{
countAquired++;
}
this.waitHandle.WaitOne(); this.waitHandle.WaitOne();
} }
public uint GetAmountOfAquires()
{
uint readCount = 0;
lock(syncLock)
{
readCount = countAquired;
}
return readCount;
}
public void Release() public void Release()
{ {
this.waitHandle.Set(); this.waitHandle.Set();

@ -14,6 +14,11 @@ namespace Sync_Ueb02_Latch
{ {
new Thread(new RaceHorse(i, startBox).Run).Start(); new Thread(new RaceHorse(i, startBox).Run).Start();
} }
// wait until all horses are ready
while(startBox.GetAmountOfAquires() < 5)
{
Thread.Sleep(20);
}
Console.WriteLine("Start..."); Console.WriteLine("Start...");
startBox.Release(); startBox.Release();
} }

Loading…
Cancel
Save