private void Run()
            {
                VConsole.WriteLine("+++ c #{0} started...", id);
                int fail = 0;
                int qi   = 0;
                int rmsg;

                do
                {
                    try {
                        int ws;
                        while ((ws = StBlockingQueue <int> .TryTakeAny(queues, qi, queues.Length,
                                                                       out rmsg, new StCancelArgs(1, shutdown))) == StParkStatus.Timeout)
                        {
                            fail++;
                        }
                        qi = ws;
                        if ((++consumptions[id] % 20000) == 0)
                        {
                            VConsole.Write("-c{0}", id);
                        }
                    } catch (StThreadAlertedException) {
                        while (StBlockingQueue <int> .TryTakeAny(queues, 0, queues.Length,
                                                                 out rmsg, new StCancelArgs(100)) != StParkStatus.Timeout)
                        {
                            consumptions[id]++;
                        }
                        break;
                    }
                } while (true);
                VConsole.WriteLine("+++ c #{0} exiting: [{1}/{2}]",
                                   id, consumptions[id], fail);
                done.Signal();
            }
            private void Run()
            {
                VConsole.WriteLine("+++ p #{0} started...", id);
                int msg   = 0;
                int fails = 0;

                do
                {
                    try {
                        if (++msg == 0)
                        {
                            msg = 1;
                        }
                        // while (!queue.TryAdd(msg, new StCancelArgs(1, shutdown))) {
                        while (StBlockingQueue <int> .TryAddAny(new StBlockingQueue <int>[] { queue },
                                                                msg, new StCancelArgs(1, shutdown)) == StParkStatus.Timeout)
                        {
                            fails++;
                        }
                        if ((++productions[id] % 20000) == 0)
                        {
                            VConsole.Write("-p{0}", id);
                        }
                    } catch (StThreadAlertedException) { }
                    //Thread.Sleep(0);
                } while (!shutdown.IsSet);
                VConsole.WriteLine("+++ p #{0} exiting: [{1}/{2}]",
                                   id, productions[id], fails);
                done.Signal();
            }
Esempio n. 3
0
        //
        // Starts the test.
        //

        internal static Action Run()
        {
            queue = new StUnboundedBlockingQueue <int>(true);
            // queue = new StBoundedBlockingQueue<int>(4 * 1024, true);
            // queue = new StArrayBlockingQueue<int>(4 * 1024, true);

            StRegisteredTake <int> regTake = queue.RegisterTake(TakeCallback <int>, null, 250, false);

            for (int i = 0; i < PRODUCERS; i++)
            {
                new Producer().Start(i, "p #" + i);
            }

            Action stop = () => {
                shutdown.Set();
                done.WaitOne();
                long ps = 0;
                for (int i = 0; i < PRODUCERS; i++)
                {
                    ps += productions[i];
                }
                VConsole.WriteLine("+++ Total: prods = {0}, cons = {1}", ps, consumptions);
                //regTake.Unregister();
            };

            return(stop);
        }
        //
        // Starts the test.
        //

        internal static Action Run()
        {
            switch (new Random(Environment.TickCount).Next(3))
            {
            case 0:
                queue = new StUnboundedBlockingQueue <int>(true);
                break;

            case 1:
                queue = new StBoundedBlockingQueue <int>(4 * 1024, true);
                break;

            default:
                queue = new StArrayBlockingQueue <int>(4 * 1024, true);
                break;
            }
            for (int i = 0; i < CONSUMERS; i++)
            {
                new Consumer().Start(i, "c #" + i);
            }
            for (int i = 0; i < PRODUCERS; i++)
            {
                new Producer().Start(i, "p #" + i);
            }

            Action stop = () => {
                shutdown.Set();
                done.WaitOne();
                long ps = 0, cs = 0;
                for (int i = 0; i < PRODUCERS; i++)
                {
                    ps += productions[i];
                }
                for (int i = 0; i < CONSUMERS; i++)
                {
                    cs += consumptions[i];
                }
                VConsole.WriteLine("+++ Total: prods = {0}, cons = {1}", ps, cs);
            };

            return(stop);
        }
        //
        // Starts the test.
        //

        internal static Action Run() {
            switch (new Random(Environment.TickCount).Next(3)) {
                case 0:
                    queue = new StUnboundedBlockingQueue<int>(true);
                    break;
                case 1:
                    queue = new StBoundedBlockingQueue<int>(4 * 1024, true);
                    break;
                default:
                    queue = new StArrayBlockingQueue<int>(4 * 1024, true);
                    break;
            }
            for (int i = 0; i < CONSUMERS; i++) {
                new Consumer().Start(i, "c #" + i);
            }
            for (int i = 0; i < PRODUCERS; i++) {
                new Producer().Start(i, "p #" + i);
            }

            Action stop = () => {
                shutdown.Set();
                done.WaitOne();
                long ps = 0, cs = 0;
                for (int i = 0; i < PRODUCERS; i++) {
                    ps += productions[i];
                }
                for (int i = 0; i < CONSUMERS; i++) {
                    cs += consumptions[i];
                }
                VConsole.WriteLine("+++ Total: prods = {0}, cons = {1}", ps, cs);
            };
            return stop;
        }