简体   繁体   中英

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

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. The same Barrier can be used for multiple phases.

(The example on the Barrier page shows four tasks all running the same code - but that's not by any means a requirement)

If you've got a variety of different tasks to wait for, then Task.WaitAll .

If lots of instances of the same task then parallel.foreach is an easy wait to run them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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