当前位置: 首页>>代码示例>>C#>>正文


C# Barrier.RemoveParticipant方法代码示例

本文整理汇总了C#中System.Threading.Barrier.RemoveParticipant方法的典型用法代码示例。如果您正苦于以下问题:C# Barrier.RemoveParticipant方法的具体用法?C# Barrier.RemoveParticipant怎么用?C# Barrier.RemoveParticipant使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Threading.Barrier的用法示例。


在下文中一共展示了Barrier.RemoveParticipant方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

        static void Main(string[] args)
        {
            // create a barrier
            Barrier barrier = new Barrier(2);

            // create a task that will complete
            Task.Factory.StartNew(() => {
                Console.WriteLine("Good task starting phase 0");
                barrier.SignalAndWait();
                Console.WriteLine("Good task starting phase 1");
                barrier.SignalAndWait();
                Console.WriteLine("Good task completed");
            });

            // create a task that will throw an exception
            // with a selective continuation that will reduce the
            // particpant count in the barrier
            Task.Factory.StartNew(() => {
                Console.WriteLine("Bad task 1 throwing exception");
                throw new Exception();

            }).ContinueWith(antecedent => {
                // reduce the particpant count
                Console.WriteLine("Reducing the barrier participant count");
                barrier.RemoveParticipant();
            }, TaskContinuationOptions.OnlyOnFaulted);

            // wait for input before exiting
            Console.WriteLine("Press enter to finish");
            Console.ReadLine();
        }
开发者ID:clp-takekawa,项目名称:codes-from-books,代码行数:31,代码来源:Listing_13.cs

示例2: Main

        static void Main(string[] args) {
            var participants = 5;

            // We create a CancellationTokenSource to be able to initiate the cancellation
            var tokenSource = new CancellationTokenSource();
            // We create a barrier object to use it for the rendez-vous points
            var barrier = new Barrier(participants,
                b => {
                    Console.WriteLine("{0} paricipants are at rendez-vous point {1}.",
                                    b.ParticipantCount,
                                    b.CurrentPhaseNumber);
                });

            for (int i = 0; i < participants; i++) {
                var localCopy = i;
                Task.Delay(1000 * localCopy + 1, tokenSource.Token)
                    .ContinueWith(_ => {
                        Console.WriteLine("Task {0} left point A!", localCopy);
                        Thread.Sleep(1000 * localCopy + 1); // Do some "work"
                        if (localCopy % 2 == 0) {
                            Console.WriteLine("Task {0} arrived at point B!", localCopy);
                            barrier.SignalAndWait(tokenSource.Token);
                        }
                        else {
                            Console.WriteLine("Task {0} changed its mind and went back!", localCopy);
                            barrier.RemoveParticipant();
                            return;
                        }
                    }, TaskContinuationOptions.NotOnCanceled)
                    .ContinueWith(_ => {
                        Thread.Sleep(1000 * localCopy + 1);
                        Console.WriteLine("Task {0} arrived at point C!", localCopy);
                        barrier.SignalAndWait(tokenSource.Token);
                    }, TaskContinuationOptions.NotOnCanceled);
            }

            Console.WriteLine("Main thread is waiting for {0} tasks!", barrier.ParticipantsRemaining - 1);
            Console.WriteLine("Press enter to cancel!");
            Console.ReadLine();
            if (barrier.CurrentPhaseNumber < 2) {
                tokenSource.Cancel();
                Console.WriteLine("We canceled the operation!");
            }
            else {
                Console.WriteLine("Too late to cancel!");
            }
            Console.WriteLine("Main thread is done!");
            Console.ReadLine();
        }
开发者ID:vikramadhav,项目名称:Certification_70-483,代码行数:49,代码来源:BarrierWithTasks.cs

示例3: CalculationInTask

        //        static int[] CalculationInTask(object p)
        static int[] CalculationInTask(int jobNumber, int partitionSize, Barrier barrier, IList<string> coll)
        {
            var data = new List<string>(coll);
            int start = jobNumber * partitionSize;
            int end = start + partitionSize;
            Console.WriteLine("Task {0}: partition from {1} to {2}", Task.CurrentId, start, end);
            int[] charCount = new int[26];
            for (int j = start; j < end; j++)
            {
                char c = data[j][0];
                charCount[c - 97]++;
            }
            Console.WriteLine("Calculation completed from task {0}. {1} times a, {2} times z", Task.CurrentId, charCount[0], charCount[25]);

            barrier.RemoveParticipant();
            Console.WriteLine("Task {0} removed from barrier, remaining participants {1}", Task.CurrentId, barrier.ParticipantsRemaining);
            return charCount;
        }
开发者ID:CNinnovation,项目名称:ParallelProgrammingFeb2016,代码行数:19,代码来源:Program.cs

示例4: Main

        static void Main(string[] args)
        {
            var participants = 5;

            Barrier barrier = new Barrier(participants + 1, // We add one for the main thread.
                b => {
                    Console.WriteLine("{0} paricipants are at rendez-vous point {1}.",
                                    b.ParticipantCount - 1, // We substract the main thread .
                                    b.CurrentPhaseNumber);
                });

            for (int i = 0; i < participants; i++) {
                var localCopy = i;
                Task.Run(() => {
                    Console.WriteLine("Task {0} left point A!", localCopy);
                    Thread.Sleep(1000 * localCopy + 1); // Do some "work"
                    if (localCopy % 2 == 0) {
                        Console.WriteLine("Task {0} arrived at point B!", localCopy);
                        barrier.SignalAndWait();
                    }
                    else {
                        Console.WriteLine("Task {0} changed its mind and went back!", localCopy);
                        barrier.RemoveParticipant();
                        return;
                    }
                    Thread.Sleep(1000 * (participants - localCopy));
                    Console.WriteLine("Task {0} arrived at point C!", localCopy);
                    barrier.SignalAndWait();
                });
            }

            Console.WriteLine("Main thread is waiting for {0} tasks!", barrier.ParticipantsRemaining - 1);
            barrier.SignalAndWait(); // Waiting at the first phase
            barrier.SignalAndWait(); // Waiting at the second phase
            Console.WriteLine("Main thread is done!");
        }
开发者ID:vikramadhav,项目名称:Certification_70-483,代码行数:36,代码来源:Program.cs

示例5: CalculationInTask

        private static void CalculationInTask(int jobNumber, int partitionSize, Barrier barrier, IList<string>[] coll, int loops, int[][] results)
        {

            LogBarrierInformation("CalculationInTask started", barrier);

            for (int i = 0; i < loops; i++)
            {
                var data = new List<string>(coll[i]);

                int start = jobNumber * partitionSize;
                int end = start + partitionSize;
                WriteLine($"Task {Task.CurrentId} in loop {i}: partition from {start} to {end}");

                for (int j = start; j < end; j++)
                {
                    char c = data[j][0];
                    results[i][c - 97]++;
                }

                WriteLine($"Calculation completed from task {Task.CurrentId} in loop {i}. " +
                    $"{results[i][0]} times a, {results[i][25]} times z");

                LogBarrierInformation("sending signal and wait for all", barrier);
                barrier.SignalAndWait();
                LogBarrierInformation("waiting completed", barrier);
            }

            barrier.RemoveParticipant();
            LogBarrierInformation("finished task, removed participant", barrier);
        }
开发者ID:ProfessionalCSharp,项目名称:ProfessionalCSharp6,代码行数:30,代码来源:Program.cs

示例6: _08_testPutTake_fixed


//.........这里部分代码省略.........
                                int elapsedTicks = (int)(currentDate.Ticks - centuryBegin.Ticks);
                                long result = 0;

                                // Wait at the barrier (start line) until all test threads have been created and are ready to go
                                _barrier.SignalAndWait();

                                // Put the data into the queue as Strings, generating a new random number each time
                                // The consumer will convert back to integers and sum them
                                // The Producer's sum should equal the Consumenr's sum at the end of the test
                                for (int j = nTrials; j > 0; --j)
                                {
                                    // If the RNG is sound then this proves that the data enqueued was dequeued
                                    int r = rand.Next(maxLongRandomSeed);

                                    _08_MMData data;

                                    // Test data string to enqueue and dequeue. Convert to a byte array. This array is a reference type so cannot be directly
                                    // passed to the View Accessor
                                    char[] encodedData = Convert.ToString(r).ToCharArray();
                                    // Store the length of the array for dequeueing later
                                    data.TextLength = encodedData.Length;
                                    // Copy the data to unmanaged memory char by char
                                    for (int k = 0; k < data.TextLength; k++) { unsafe { data.Text[k] = encodedData[k]; } }

                                    mmMain.Put(data);
                                    result += r;

                                }
                                // Atomically store the computed checksum
                                // Comment out for Test 01 as we have already incremented it
                                Interlocked.Add(ref putSum, result);

                                // Wait at the barrier (finish line) until all test threads have been finished
                                _barrier.SignalAndWait();

                            }
                            catch (Exception unexpected)
                            {
                                _barrier.RemoveParticipant();
                                Console.Write("Producer thread terminated after catching an exception from MMChannel 'put()'");
                                Console.Write(unexpected);
                            }
                        }
                    )).Start();

                    #endregion Producer Lamda declaration

                }

                // Wait for all the threads to be ready
                _barrier.SignalAndWait();

                // Wait for all the threads to finish
                _barrier.SignalAndWait();

                // calculate the number of ticks elapsed during the test run
                long elapsedTime = Interlocked.Read(ref timerEndTime) - Interlocked.Read(ref timerStartTime);
                Console.WriteLine("Intermediate Result of _08_testPutTake_fixed() - elapsed time = {0} timer ticks for {1} producer/consumer pairs and {2} Messages",
                    elapsedTime, nPairs, nTrials);

                // Calculate the number of ticks per item enqueued and dequeued - the throughput of the queue
                // A single tick represents one hundred nanoseconds or one ten-millionth of a second.
                // There are 10,000 ticks in a millisecond.
                long ticksPerItem = elapsedTime / (nPairs * (long)nTrials);
                TimeSpan elapsedSpan = new TimeSpan(ticksPerItem);
                double milliSeconds = elapsedSpan.TotalMilliseconds;
                long nanoSeconds = ticksPerItem * 100;
                long throughput = 1000000000 / nanoSeconds;

                // Compares the checksum values computed to determine if the data enqueued was exactly the data dequeued
                Console.WriteLine("1st Result of _08_testPutTake_fixed() = (data enqueued = {0} after {1} trials each by {2} pairs of producers/consumers",
                    Interlocked.Read(ref putSum), nTrials, nPairs);

                Console.WriteLine("2nd Result of _08_testPutTake_fixed() = (Average latency = {0} timer ticks <= Threshold value {1}) = {2}",
                    Interlocked.Read(ref ticksPerItem),
                    AVERAGE_THROUGHPUT_THRESHOLD_TICKS,
                    Interlocked.Read(ref ticksPerItem) <= AVERAGE_THROUGHPUT_THRESHOLD_TICKS);

                Console.WriteLine("_08_testPutTake_fixed Throughput = {0} messages per second ", throughput);

                Console.WriteLine("_08_testPutTake_fixed {0} timer ticks = {1} nanoseconds or {2} milliseconds",
                    ticksPerItem, nanoSeconds, milliSeconds);

                Console.WriteLine("\nEnd of Test Run No. {0} in Test Suite No. {1}\n", initTestRunNumber, initTestSuiteNumber);

            }
            catch (Exception unexpected)
            {
                Console.Write(unexpected);
                throw;
            }
            finally
            {
                // Temporarily delay disposing the queue and its IPC artefacts to allow the consumers to finish draining the queue
                // This will be fixed by waiting in an interrupible loop for the mutex inside the queue and checking if shutdown
                // Thread.Sleep(1000);
                mmMain.Report();
                mmMain.Dispose();
            }
        }
开发者ID:tapman,项目名称:ipc-queue,代码行数:101,代码来源:ProducerTestRunner.cs

示例7: RunBarrierTest7_Bug603035

        private static bool RunBarrierTest7_Bug603035()
        {
            TestHarness.TestLog("*RunBarrierTest7_Bug603035");
            bool failed = false;
            for (int j = 0; j < 100 && !failed; j++)
            {
                Barrier b = new Barrier(0);
               
                Action[] actions = Enumerable.Repeat((Action)(() =>
                {
                    for (int i = 0; i < 400; i++)
                    {
                        try
                        {
                            b.AddParticipant();
                            b.RemoveParticipant();
                        }
                        catch
                        {
                            failed = true;
                            break;
                        }
                    }

                }), 4).ToArray();
                Parallel.Invoke(actions);
                if (b.ParticipantCount != 0)
                    failed = true;
            }

            if (failed)
            {
                TestHarness.TestLog("Bug603035 failed");
                return false;
            }
            TestHarness.TestLog("Bug603035 succeeded");
            return true;

        }
开发者ID:modulexcite,项目名称:IL2JS,代码行数:39,代码来源:BarrierTests.cs


注:本文中的System.Threading.Barrier.RemoveParticipant方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。