Revious
Revious

Reputation: 8156

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.

Upvotes: 2

Views: 136

Answers (2)

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239824

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)

Upvotes: 5

Jon Egerton
Jon Egerton

Reputation: 41589

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.

Upvotes: 1

Related Questions