Exemplo n.º 1
0
 static void ConsumeAll()
 {
     while (true)
     {
         _gate.WaitOne();
         if (_queueForGate.Any())
         {
             ValuableRessource result = _queueForGate.Dequeue();
             Console.WriteLine($"# Gate dequeued: {result.Key}:{result.Value}");
             Thread.Sleep(10);
         }
     }
 }
Exemplo n.º 2
0
 static void Consume(string name)
 {
     lock (_token)
     {
         while (true)
         {
             Monitor.Wait(_token);
             if (_queue.Any())
             {
                 ValuableRessource result = _queue.Dequeue();
                 Console.WriteLine($"{name} dequeued: {result.Key}:{result.Value}");
             }
         }
     }
 }
Exemplo n.º 3
0
 static void ConsumeTurnstile()
 {
     while (true)
     {
         _turnstile.WaitOne();
         lock (_token2)
         {
             if (_queueForTurnstile.Any())
             {
                 ValuableRessource result = _queueForTurnstile.Dequeue();
                 Console.WriteLine($"+ Turn dequeued: {result.Key}:{result.Value}");
                 Thread.Sleep(10);
             }
         }
     }
 }
Exemplo n.º 4
0
        static void FetchRessource(int fetcher, int target, int distance)
        {
            try
            {
                if (_ressources.Any(x => x.Key == target))
                {
                    Console.WriteLine($"Fetcher-{fetcher} Ressource found ... takes {distance} to reach");
                    Thread.Sleep(distance);

                    Console.WriteLine($"Fetcher-{fetcher} - Checking Lock");
                    lock (_token)
                    {
                        Console.WriteLine($"Fetcher-{fetcher} - Locked Ressource");
                        Thread.Sleep(100);

                        ValuableRessource taken = _ressources.FirstOrDefault(x => x.Key == target);
                        _ressources.Remove(taken);


                        string result = taken != null
                                    ? taken.Value + " is taken"
                                    : "Ressource was removed before";

                        Console.WriteLine($"Fetcher-{fetcher} - " + result);
                    }
                    Console.WriteLine($"Fetcher-{fetcher} - Release Lock");
                }
                else
                {
                    Console.WriteLine($"Fetcher-{fetcher} - Ressource not found");
                }
            }
            catch (System.Exception)
            {
            }
        }
Exemplo n.º 5
0
        static void FetchRessource(int fetcher, int target, int distance)
        {
            bool abortFetch = false;

            try
            {
                Console.WriteLine($"Fetcher-{fetcher} waiting for ReadLock");
                _token.EnterReadLock();
                Console.WriteLine($"Fetcher-{fetcher} >>> EnterReadLock");
                Console.WriteLine($"Fetcher-{fetcher} Checking Ressource");
                Thread.Sleep(50);
                if (_ressources.Any(x => x.Key == target))
                {
                    Console.WriteLine($"Fetcher-{fetcher} - Ressource found -> will fetch");
                }
                else
                {
                    Console.WriteLine($"Fetcher-{fetcher} - Ressource not found -> Abort");
                    abortFetch = true;
                }
            }
            finally
            {
                _token.ExitReadLock();
                Console.WriteLine($"Fetcher-{fetcher} <<< ExitReadLock");
            }
            if (abortFetch)
            {
                return;
            }

            Console.WriteLine($"Fetcher-{fetcher} Ressource found ... takes {distance} to reach");
            Thread.Sleep(distance);

            try
            {
                Console.WriteLine($"Fetcher-{fetcher} waiting for WriteLock");
                _token.EnterWriteLock();
                Console.WriteLine($"Fetcher-{fetcher} >>> EnterWriteLock");

                if (_ressources.Any(x => x.Key == target))
                {
                    Console.WriteLine($"Fetcher-{fetcher} - Ressource still present -> taking");
                    Thread.Sleep(100);

                    ValuableRessource taken = _ressources.FirstOrDefault(x => x.Key == target);
                    _ressources.Remove(taken);

                    Console.WriteLine($"Fetcher-{fetcher} - Ressource taken");
                }
                else
                {
                    Console.WriteLine($"Fetcher-{fetcher} - Ressource already teaken -> abort");
                }
            }
            finally
            {
                _token.ExitWriteLock();
                Console.WriteLine($"Fetcher-{fetcher} <<< ExitWriteLock");
            }
        }