Exemplo n.º 1
0
        static void Run <T>(IEnumerable <IndexedValue <T> > items, DegreeOfParallelism degreeOfParallelism, CancellationToken cancellationToken = default)
        {
            IOrderedQueue <IndexedValue <T> > orderedQueue =
                new LockFreeOrderedQueue <IndexedValue <T> >((x, y) => x.Index.CompareTo(y.Index));

            var enqueuingCompleted = false;

            using var consumerLinkedCancellationTokenSource  = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
            using var producersLinkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);

            var queueWorker = new Thread(() =>
            {
                try
                {
                    HandleOrderedQueue(orderedQueue, () => enqueuingCompleted, producersLinkedCancellationTokenSource.Token);
                }
                catch (OperationCanceledException)
                {
                    // ignore this one
                }
                catch (Exception)
                {
                    consumerLinkedCancellationTokenSource.Cancel();
                }
            });

            queueWorker.Start();

            var encodingExceptions = ParallelExecution.ForEach(
                items,
                handleItem: item =>
            {
                // if (item.Index == 27) { throw new Exception("!!!ParallelExecution!!!T_T"); }
                Console.WriteLine($"{Thread.CurrentThread.Name}: starts working on item {item.Index}");

                // emulate encoding work
                Thread.Sleep(300);     //  * (item.Index % 2 == 0 ? 4 : 1));

                // Console.WriteLine($"{Thread.CurrentThread.Name}: ends working on item {item.Index}");
                orderedQueue.Enqueue(item);
                Semaphore.Release();
            },
                degreeOfParallelism,
                consumerLinkedCancellationTokenSource.Token);

            enqueuingCompleted = true;

            if (encodingExceptions.Count > 0)
            {
                if (!encodingExceptions.OfType <OperationCanceledException>().Any())
                {
                    producersLinkedCancellationTokenSource.Cancel();
                }

                LogEncodingExceptions(encodingExceptions);
            }

            queueWorker.Join();
        }
Exemplo n.º 2
0
        public void Enqueue_sorts_elements(int[] values)
        {
            var qrderedQueue = new LockFreeOrderedQueue <int>((x, y) => x.CompareTo(y));

            values.ForEach(qrderedQueue.Enqueue);

            var expected = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            Assert.Equal(expected, qrderedQueue.ToArray());
        }