Barrier in .NET 4.0

Barrier is a new type introduced in .NET 4.0. It allows the user to define the synchronization primitives and lets your Threads to run code simultaneously in predefined phases.

A Barrier defines a phases in such a way that the Thread executing some set of code reaches a certain point where it is stopped and sent to wait until all the Threads running in Barrier gets the Signal. The set of code that it executes is called Pre-Phaze work until each of them reaches a Barrier. Once all the Thread reaches the Barrier, it executes the Post-Phaze code.

Thus Barrier class is used to very specific case, in which you require to specify a barrier for the Threads to stop and wait for other Threads to finish the execution and finally run a method specified as callback to the Barrier as Post – Phaze code.

Lets look the same in code :

public static Barrier barrier = new Barrier(5, e =>
{
    Console.WriteLine("All threads Finishes");
});
static void Main(string[] args)
{
    Thread t1 = new Thread(() => ProcessPhaze1());
    Thread t2 = new Thread(() => ProcessPhaze1());
    Thread t3 = new Thread(() => ProcessPhaze1());
    Thread t4 = new Thread(() => ProcessPhaze1());
    Thread t5 = new Thread(() => ProcessPhaze1());

    t1.Start();
    t2.Start();
    t3.Start();
    t4.Start();
    t5.Start();

    Console.ReadLine();
}

public static void ProcessPhaze1()
{
    Thread.Sleep(1000);

    barrier.SignalAndWait(2000);
}

Here each thread calls ProcessPhaze1 from main. Thus the ProcessPhaze1 will be called in parallel by 5 threads. Once each of them finishes execution (for simplicity I have just used Thread.Sleep) it signals the Barrier to wait and thus it stops itself. As shown here, you can specify the Timeout value for the SignalAndWait to ensure that deadlock does not occur.

Once all Threads are signalled, it calls the callback specified as argument in Barrier. The first argument 5 specifies how many threads are participant of the Barrier. Here I am using 5 Threads. If I change the argument to 2, it will print the Phaze2 callback twice.

This is because the argument specifies after how many signals are received, it should call this callback. After each callback is executed, the Barrier PhazeNumber gets incremented. You can get the value of Phaze number from barrier.CurrentPhaseNumber.

Barrier also supports dynamic addition and removal of participants too.

I hope this post helps you.

Thanks for reading.

Abhishek Sur

Abhishek Sur is a Microsoft MVP since year 2011. He is an architect in the .NET platform. He has profound theoretical insight and years of hands on experience in different .NET products and languages. He leads the Microsoft User Group in Kolkata named KolkataGeeks, and regularly organizes events and seminars in various places for spreading .NET awareness. He is associated with the Microsoft Insider list on WPF and C#, and is in constant touch with product group teams. He blogs at http://www.abhisheksur.com His Book : Visual Studio 2012 and .NET 4.5 Expert Development Cookbook. Follow Abhishek at Twitter : @abhi2434