示例#1
0
        public void FillBottle()
        {
            lock (o)
            {
                while (isManagerHelperDoing)
                {
                    waitForQueuePulse.Wait(o);
                }

                table.FillBottleOfWine();

                for (int i = 0; i < n; i++)
                {
                    if (isWaitingForWine[i])
                    {
                        QueuePulse.Enqueue(conditionVariable[i]);
                    }
                }

                if (QueuePulse.Count != 0)
                {
                    isManagerHelperDoing = true;
                    helperQueue.Pulse();
                }
            }
        }
示例#2
0
        public void StartTellingStory(int knightTelling, List <int> knightListening)
        {
            lock (o)
            {
                while (isManagerHelperDoing)
                {
                    waitForQueuePulse.Wait(o);
                }

                while (isListening[knightTelling] == true)
                {
                    conditionVariable[knightTelling].Wait(o);
                }
                //--------------------------------------------------------

                ConsoleWriter.WriteMessage($"Starting telling Story by Knight : {knightTelling} ", ConsoleColor.DarkGreen);

                isTelling[knightTelling] = true;

                for (int i = 0; i < knightListening.Count; i++)
                {
                    int index = knightListening[i];
                    if (!isTelling[index])
                    {
                        if (!isListening[index])
                        {
                            isListening[index] = true;
                        }
                        nListening[index]++;
                    }
                }
            }
        }
示例#3
0
 public void WaitForEndPlaying(int anyonePlaying)
 {
     lock (jankielPlayLock)
     {
         nPlaying = anyonePlaying;
         mainThreadQueue.Wait(jankielPlayLock);
         nPlaying = -1;
     }
 }
示例#4
0
        public void Test1()
        {
            var  mutex     = new object();
            var  cv        = new ConditionVariable <int>();
            bool ready     = false;
            bool processed = false;

            // Main
            {
                var worker = new Thread(new ThreadStart(WorkerThread)).Run();

                // send data to the worker thread
                lock (mutex)
                {
                    ready = true;
                    cv.NotifyOne(mutex, 1);
                }

                // wait for the worker
                lock (mutex)
                {
                    var value = cv.Wait(mutex, x => processed);
                    Assert.Equal(value, 42);
                }

                worker.Join();
            }

            void WorkerThread()
            {
                // Wait until main() sends data
                lock (mutex)
                {
                    var value = cv.Wait(mutex, x => ready);
                    Assert.Equal(value, 1);

                    // after the wait, we own the lock.
                    cv.NotifyOne(mutex, 42);

                    // Send data back to main()
                    processed = true;
                }
            }
        }
示例#5
0
        public void Test0()
        {
            var cv        = new ConditionVariable();
            var data      = (string?)null;
            var ready     = false;
            var processed = false;

            // Main
            {
                var worker = new Thread(new ThreadStart(WorkerThread)).Run();

                data = "Example data";

                // send data to the worker thread
                ready = true;
                cv.NotifyOne();

                // wait for the worker
                cv.Wait(() => processed);

                // "Back in main(), data = " << data << '\n';
                Assert.Equal(data, "Changed data");

                worker.Join();
            }

            void WorkerThread()
            {
                // Wait until main() sends data
                cv.Wait(() => ready);

                // after the wait, we own the lock.
                data = "Changed data";

                // Send data back to main()
                processed = true;

                // Should be notifying outside of the lock but C# Monitor.Pulse is
                // not quite the same as std::condition_variable.notifiy_all()
                cv.NotifyOne();
            }
        }
示例#6
0
 public MessageFromClient ServerReceive()
 {
     lock (obj)
     {
         if (messages.Count == 0)
         {
             empty.Wait(obj);
         }
         MessageFromClient message = messages.Dequeue();
         return(message);
     }
 }
 private void EnterReading()
 {
     lock (_lock)
     {
         while (_isWriting || _readersNumber >= _maxReaders)
         {
             _reading.Wait(_lock);
         }
         _readersNumber++;
         _readersInARowCounter++;
     }
 }
        private void EnterWriting()
        {
            lock (_lock)
            {
                while (_isWriting || _readersNumber > 0)
                {
                    _writing.Wait(_lock);
                }

                _isWriting = true;
            }
        }
示例#9
0
 public bool WaitIfNecessary(string queue)
 {
     lock (jankielLock)
     {
         //System.Console.WriteLine($"{queue} : waitIfNecessary for {neighborCount}, counter {counter}");
         if (counter > 0)
         {
             mainThreadQueue.Wait(jankielLock);
         }
         counter = neighborCount;
         bool B = bCount;
         bCount = false;
         return(B);
     }
 }
示例#10
0
        public void DequeuePulse()
        {
            lock (o)
            {
                while (!isManagerHelperDoing)
                {
                    helperQueue.Wait(o);
                }

                if (QueuePulse.Count != 0)
                {
                    QueuePulse.Dequeue().Pulse();
                }
                else
                {
                    isManagerHelperDoing = false;
                    waitForQueuePulse.PulseAll();
                }
            }
        }
        public void StartDrinking(int i)
        {
            lock (obj)
            {
                bool check = true;
                do
                {
                    if (cucumbersState[((N + i - 1) % N) / 2] == 0)
                    {
                        Console.WriteLine("Run out of cucumbers");
                        cucumbersRefillCond.Wait(obj);
                    }
                    if (wineState == 0)
                    {
                        Console.WriteLine("Run out of wine");
                        wineRefillCond.Wait(obj);
                    }
                    check = (cucumbersState[((N + i - 1) % N) / 2] == 0); // sprawdzamy jeszcze raz stan ogórków, podczas czekania na wino mogły sie skończyć
                    check = (wineState == 0);
                } while (check);
                cucumbersState[((N + i - 1) % N) / 2]--;
                wineState--;

                if (isBusy[i] == true || isBusy[(i + 1) % N] == true)
                {
                    isWaiting[i] = true;
                    hisTurnCond[i].Wait(obj);
                    isWaiting[i] = false;
                }

                isBusy[i]           = true;
                isBusy[(i + 1) % N] = true;
                if (waitPrev)
                {
                    waitPrev = false;
                    hisTurnCond[(N + i - 2) % N].Pulse();
                }
            }
        }