ConcurrentQueue represents a thread-safe first in-first out (FIFO) collection.
using System; using System.Collections.Concurrent; using System.Threading; using System.Threading.Tasks; class MainClass { static void Main () { ConcurrentQueue<int> cq = new ConcurrentQueue<int>(); for (int i = 0; i < 10000; i++) cq.Enqueue(i); int result; if (!cq.TryPeek(out result)) { Console.WriteLine("CQ: TryPeek failed when it should have succeeded"); } else if (result != 0) { Console.WriteLine("CQ: Expected TryPeek result of 0, got {0}", result); } int outerSum = 0; Action action = () => { int localValue; int localSum = 0; while (cq.TryDequeue(out localValue)) localSum++; Interlocked.Add(ref outerSum, localSum); }; Parallel.Invoke(action, action, action, action); Console.WriteLine( outerSum); } }