Commits

Anonymous committed 2f46faa

Locking queue implementation

  • Participants
  • Parent commits a529c16
  • Branches Locking

Comments (0)

Files changed (1)

File ConsoleApplication2/Program.cs

     /// </summary>
     class Program
     {
-        private const int s_Iterations = 1000000000;
+        private const int s_Iterations = 100000000;
 
         static void Main(string[] args)
         {
 
         private static void Go(int instance)
         {
-            ConcurrentQueue<long> queue = new ConcurrentQueue<long>();
+            Queue<long> queue = new Queue<long>();
             Thread producer = new Thread(() => Producer(queue));
             Thread consumer = new Thread(() => Consumer(queue, instance));
 
             consumer.Start();
         }
 
-        static void Producer(ConcurrentQueue<long> queue)
+        static void Producer(Queue<long> queue)
         {
             long number = 0;
             while (true)
             {
-                queue.Enqueue(number++);
+                lock (queue)
+                {
+                    queue.Enqueue(number++);
+                }
                 for (int i = 0; i < 5; i++)
                 {
                     number++;
             }
         }
 
-        static void Consumer(ConcurrentQueue<long> queue, int instance)
+        static void Consumer(Queue<long> queue, int instance)
         {
             DateTime lastUpdate = DateTime.Now;
             long expected = 0;
             while (true)
             {
-                long observed;
-                while (!queue.TryDequeue(out observed))
+                long observed = -1;
+                while (observed == -1)
                 {
+                    lock (queue)
+                    {
+                        if (queue.Any())
+                        {
+                            observed = queue.Dequeue();
+                        }
+                    }
                 }
 
                 if (observed != expected)