public IBufferRegistration GetBuffer()
 {
     // this blocks until a buffer is free
     guard.Wait();
     // can now get buffer so make sure we're the only thread manipulating
     // the list of buffers
     lock (buffers)
     {
         IBufferRegistration freeBuffer = null;
         // look for a free buffer
         foreach (LOHBuffer buffer in buffers)
         {
             if (!buffer.InUse)
             {
                 buffer.InUse = true;
                 freeBuffer   = new BufferReservation(this, buffer);
             }
         }
         // no free buffer so allocate a new one
         if (freeBuffer == null)
         {
             var buffer = new LOHBuffer();
             buffer.InUse = true;
             buffers.Add(buffer);
             freeBuffer = new BufferReservation(this, buffer);
         }
         return(freeBuffer);
     }
 }
        public IBufferRegistration GetBuffer()
        {
            guard.Wait();
            // can get buffer
            lock (buffers)
            {
                IBufferRegistration freeBuffer = null;
                foreach (LOHBuffer buffer in buffers)
                {
                    if (!buffer.InUse)
                    {
                        buffer.InUse = true;
                        freeBuffer   = new BufferReservation(this, buffer);
                    }
                }

                if (freeBuffer == null)
                {
                    var buffer = new LOHBuffer();
                    buffer.InUse = true;
                    buffers.Add(buffer);
                    freeBuffer = new BufferReservation(this, buffer);
                }

                return(freeBuffer);
            }
        }
示例#3
0
        static void Main(string[] args)
        {
            var pool = new BufferPool(2);

            IBufferRegistration reg = pool.GetBuffer();

            Task.Factory.StartNew(() =>
            {
                IBufferRegistration reg2 = pool.GetBuffer();
                Console.WriteLine("task got buffer");
            });

            Console.WriteLine("press enter to dispose reg");
            Console.ReadLine();
            reg.Dispose();

            Console.ReadLine();
        }