HOME       UP       PREV       NEXT (Concurrency With Channels)  

Kiwi Library: One-Place Buffers

        public class Channel
        {
          T datum;
          volatile bool emptyflag = true;

          public void Write(T v)
            {
                lock (this)
                {
                    while (!emptyflag) { Kiwi.NoUnroll(); Monitor.Wait(this); }
                    datum = v;
                    emptyflag = false;
                    Monitor.PulseAll(this);
                }
            }

         public T Read()
            {
                T r;
                lock (this)
                {
                    while (emptyflag) { Kiwi.NoUnroll(); Monitor.Wait(this); }
                    emptyflag = true;
                    r = datum;
                    Monitor.PulseAll(this);
                }
                return r;
            }
        }
    }

18: (C) 2011, DJ Greaves, S Singh, University of Cambridge, Computer Laboratory.