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