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

前端 未结 5 767
生来不讨喜
生来不讨喜 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条回答
  •  梦毁少年i
    2020-12-19 03:13

    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.

提交回复
热议问题