Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Barrier in C# 4.0 and WaitHandle in C# 3.0?

I am picking up C# 4.0 and one of the things which is confusing me, is the barrier concept.

Is this not just like using the WaitAll method of WaitHandle? Doesn't that wait for all threads to finish?

I learnt the barrier construct from this page: http://www.managed-world.com/archive/2009/02/09/an-intro-to-barrier.aspx

However, it seems just like the WaitAll method. What am I missing? What's the difference here?

Thanks.

like image 686
GurdeepS Avatar asked Jun 13 '09 16:06

GurdeepS


People also ask

What is a barrier in C?

In parallel computing, a barrier is a type of synchronization method. A barrier for a group of threads or processes in the source code means any thread/process must stop at this point and cannot proceed until all other threads/processes reach this barrier.

What is barrier Wikipedia?

A barrier or barricade is a physical structure which blocks or impedes something.

What is a fence instruction?

In computing, a memory barrier, also known as a membar, memory fence or fence instruction, is a type of barrier instruction that causes a central processing unit (CPU) or compiler to enforce an ordering constraint on memory operations issued before and after the barrier instruction.

How does Pthread barrier Wait work?

The pthread_barrier_wait() function shall synchronize participating threads at the barrier referenced by barrier. The calling thread shall block until the required number of threads have called pthread_barrier_wait() specifying the barrier.


1 Answers

It sounds like you are curious as to why a Barrier would be preferred over a WaitHandle + WaitForAll derivative? Both can achieve a similar goal if structured properly.

I'm not extremely familiar with Barrier but one advantage that jumps out at me is a resource issue. To synchronize N threads with a Barrier requires only a single Barrier instance. To synchronize N threads via a WaitHandle and WaitAll requires N handles. These resources are cheap but not free. Reducing the number of resources to synchronize a group of threads has it's advantages.

like image 127
JaredPar Avatar answered Sep 20 '22 22:09

JaredPar