This recipe illustrates another interesting synchronization construct called Barrier
. The Barrier
construct helps to organize several threads to meet at some point in time, providing a callback that will be executed each time the threads have called the SignalAndWait
method.
To step through this recipe, you will need Visual Studio 2012. There are no other prerequisites. The source code for this recipe could be found at BookSamples\Chapter2\Recipe7
.
To understand the use of the Barrier
construct, perform the following steps:
Start Visual Studio 2012. Create a new C# Console Application project.
In the
Program.cs
file, add the followingusing
directives:using System; using System.Threading;
Below the
Main
method, add the following code:static Barrier _barrier = new Barrier(2, b => Console.WriteLine("End of phase {0}",b.CurrentPhaseNumber + 1)); static void PlayMusic(string name, string message,int seconds) { for (int i = 1; i <...