简体   繁体   English

同步执行多个任务

[英]Synchronizing execution of multiple tasks

How do I sync the execution of multiple tasks? 如何同步执行多个任务?

I need all task to reach the same instruction before they can continue their execution. 在继续执行之前,我需要所有任务达到相同的指令。

Every task has to wait for the others to reach that instruction before going on. 每个任务都必须等待其他任务到达该指令才能继续。

Sounds like you're looking for Barrier 听起来您正在寻找Barrier

Enables multiple tasks to cooperatively work on an algorithm in parallel through multiple phases. 使多个任务可以通过多个阶段并行地协同工作于一个算法。

and, 和,

A group of tasks cooperate by moving through a series of phases, where each in the group signals it has arrived at the Barrier in a given phase and implicitly waits for all others to arrive. 一组任务通过一系列阶段进行协作,其中该组中的每个阶段都表明它已在给定阶段到达Barrier ,并隐式等待所有其他阶段到达。 The same Barrier can be used for multiple phases. 相同的Barrier可用于多个阶段。

(The example on the Barrier page shows four tasks all running the same code - but that's not by any means a requirement) (“ Barrier页面上的示例显示了四个都运行相同代码的任务-但这绝不是必需的)

If you've got a variety of different tasks to wait for, then Task.WaitAll . 如果您要等待各种不同的任务,请使用Task.WaitAll

If lots of instances of the same task then parallel.foreach is an easy wait to run them. 如果同一任务的许多实例,那么parallel.foreach是运行它们的简便方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM