using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
class MainClass
{
static void Main ()
{
ConcurrentQueue cq = new ConcurrentQueue();
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);
}
}