障碍类c#

时间:2014-07-26 20:27:08

标签: c# multithreading barrier

我理解C#中使用的Barrier类。但是,在下面的代码中,我不明白为什么SignalAndWait()被调用了两次?这项任务的召唤不够吗?该代码基本上模拟了三个朋友(或任务)从A到B,B到C,以及一些从B回到A而不去C的情况。 请帮帮我。顺便说一下,这段代码来自书:MCSD Certification Exam Toolkit(70-483)。非常感谢你!

static void Main(string[] args) 
{
    var participants = 5;
    Barrier barrier = new Barrier(participants + 1,
        b => { // This method is only called when all the paricipants arrived.
            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)); // Do some "morework"
            Console.WriteLine("Task {0} arrived at point C!", localCopy);
            barrier.SignalAndWait(); 
        });
    }

    Console.WriteLine("Main thread is waiting for {0} tasks!",
    barrier.ParticipantCount - 1);
    barrier.SignalAndWait(); // Waiting at the first phase
    barrier.SignalAndWait(); // Waiting at the second phase
    Console.WriteLine("Main thread is done!");
}

1 个答案:

答案 0 :(得分:2)

您还会看到行Console.WriteLine("{0} paricipants are at rendez-vous point {1}.",...)执行两次。

单个Barrier实例用于在B和C处进行渲染。(剩余的)taks呼叫SignalAndWait()以标记他们到达B和C,因此两次呼叫。

打扮代码:

   if (localCopy % 2 == 0) 
   {
        ...
        barrier.SignalAndWait();       // arrival at B
    }
    else 
    {
        ...
        barrier.RemoveParticipant();   // return to A
        return;
    }
    ...
    barrier.SignalAndWait();           // arrival at C