예제 #1
0
        public void Run()
        {
            Console.WriteLine("Queue test...");

            ProcessorSlot.Set(Environment.ProcessorCount - 1);
            CountTracking();
            Subscribe();

            // create ItemCount items and push them in the queue.
            Stopwatch stopwatch = new Stopwatch();
            var       items     = Enumerable.Range(1, ItemCount).Select(
                i => new Item()
            {
                Id = Guid.NewGuid(), Code = i, Amount = 100m * i
            }).ToArray();

            InitTiming();
            stopwatch.Start();
            for (int i = 0; i < ItemCount / 1; i++)
            {
                Shield.InTransaction(() => {
                    for (int j = 0; j < 1; j++)
                    {
                        _queue.Append(items[i * 1 + j]);
                    }
                });
            }

            Console.WriteLine("..all items added, waiting.");
            _barrier.SignalAndWait();
            var time = stopwatch.ElapsedMilliseconds;

            Console.WriteLine(" -- completed in {0} ms.", time);
        }
예제 #2
0
 private void Subscribe()
 {
     Shield.Conditional(() => ProcessorSlot.Free && _queue.Any(), () => {
         Interlocked.Increment(ref _subscribeCount);
         while (ProcessorSlot.Free)
         {
             ProcessorSlot.Take();
             Shield.SideEffect(() => Task.Factory.StartNew(Process));
         }
     });
 }
예제 #3
0
        private void ProcessInt()
        {
            int yieldCount = 0;

            Item[] items;
            while (yieldCount++ < 10)
            {
                if ((items = Shield.InTransaction(() =>
                {
                    Interlocked.Increment(ref _processTestCount);
                    if (!_queue.Any())
                    {
                        return(null);
                    }
                    return(_queue.Consume.Take(10).ToArray());
                })) != null)
                {
                    yieldCount = 0;
                    foreach (var item in items)
                    {
                        Interlocked.Increment(ref _processBodyCount);
                        // do a transaction, or whatever.
                        //if (item.Code % 10000 == 0)
                        //    Shield.SideEffect(() => Console.WriteLine("-- Item {0}", item.Code));
                        Interlocked.Increment(ref _processed);
                    }
                    CountTracking();
                }
                else
                {
                    Thread.Yield();
                }
            }

            Shield.InTransaction(() => {
                ProcessorSlot.Release();
                Shield.SideEffect(() => {
                    if (_processed == ItemCount && Interlocked.Increment(ref _processed) == ItemCount + 1)
                    {
                        _barrier.SignalAndWait();
                    }
                });
            });
        }