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

前端 未结 5 786
生来不讨喜
生来不讨喜 2020-12-19 02:43

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

5条回答
  •  忘掉有多难
    2020-12-19 02:57

    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;-).

提交回复
热议问题