10

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.

Noldorin
  • 144,213
  • 56
  • 264
  • 302
GurdeepS
  • 65,107
  • 109
  • 251
  • 387
  • I'm not familiar with the Barrier class, but reading over that link, I can't see any differences either. They are either quite subtle, or I like you am missing something obvious here. – Noldorin Jun 13 '09 at 16:40

5 Answers5

11

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.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • I think you've nailed it (re N handles point, which is a good one). – GurdeepS Jun 13 '09 at 16:57
  • Are you sure the WaitHandle approach needs N WaitHandles? I think the following code works perfectly and still only requires a single object. (see code on this page since in another answer since I'm out of space) – ultimA Feb 16 '12 at 12:32
6

See Barrier. It waits for a group of multiple threads to reach certain point, instead of one. It is often used in scientific computing and simulation to represent time "ticks."

Imagine a 1000 x 1000 x 1000 grid of cubes representing cubic mile of air. At time zero a given unit cube gets affected by its neighbors' various parameters like temp and pressure. Once everyone computes time 1, you do the same for time 2... You get a weather simulation. Similar story for nuclear simulation.

There's also a variation of barrier called CyclicBarrier where it accepts threads that didn't take off from the start line and let it join back into the group after some time. It's not clear from the documentation whether C# 4's Barrier is a cyclic barrier, but there's a property called ParticipantsRemaining.

Eugene Yokota
  • 94,654
  • 45
  • 215
  • 319
  • WaitHandle is capable of that too, using the WaitAll method: http://msdn.microsoft.com/en-us/library/system.threading.waithandle.waitall.aspx – Noldorin Jun 13 '09 at 16:39
6

Barrier offers a higher level of abstraction and convenience: a single SignalAndWait call is all each thread needs to do, rather than having to know which handle in the array it should signal (or use a mutex to find and increment the "next available spot in the array" and signal that) and to have to first signal and then WaitAll.

In the end of course you can perform the same synchronization task by appropriate use of other synchronization mechanisms, but for such a common usage pattern as Barrier embodies, it's handy to have such a convenient and fool-proof solution already there and neatly packaged up;-).

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
1

WaitFor is a Transact SQL statement. It blocks the execution of a batch, stored procedure, or transaction until a specified time or time interval is reached, or a specified statement modifies or returns at least one row.

A Barrier is a synchronization primitive that enforces the stopping of execution between a number of threads or processes at a given point and prevents further execution until all threads or processors have reached the given point.

If you are referring to WaitAll, WaitAll requires you to maintain an array of WaitHandles. In that sense, barrier is a little simpler to use. However, I agree the two methods do look remarkably similar.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • I think he means the WaitAll method: http://msdn.microsoft.com/en-us/library/system.threading.waithandle.waitall.aspx. (There are also the WaitOne and WaitAny methods.) – Noldorin Jun 13 '09 at 16:42
  • Sorry, I did mean the WaitAll() method. Just got my terms mixed up :) – GurdeepS Jun 13 '09 at 16:56
0

Seems like a counted waithandle to me. Gives you the convenience to say "when the number of threads waiting on this lock becomes X, let them all go." It's nothing you can't do with another construct, but it does seem convenient.

JP Alioto
  • 44,864
  • 6
  • 88
  • 112
  • If that's the case, then yeah, a semaphore could do the job just the same. – Noldorin Jun 13 '09 at 16:44
  • Well, a counted semaphore let's a certain number through without respect to arrival time and then holds the rest. So, it's a bit of a reverse semaphore. Rather than let X through at any given time, it makes X wait an then let's them all through at once. – JP Alioto Jun 13 '09 at 17:05