views:

27

answers:

1

I am still fairly new to parallel computing so I am not too sure which tool to use for the job.

I have a System.Threading.Tasks.Task that needs to wait for n number number of tasks to finish before starting. The tricky part is some of its dependencies may start after this task starts (You are guaranteed to never hit 0 dependent tasks until they are all done).

Here is kind of what is happening

  1. Parent thread creates somewhere between 1 and (NUMBER_OF_CPU_CORES - 1) tasks.
  2. Parent thread creates task to be run when all of the worker tasks are finished.
  3. Parent thread creates a monitoring thread
  4. Monitoring thread may kill a worker task or spawn a new task depending on load.

I can figure out everything up to step 4. How do I get the task from step 2 to wait to run until any new worker threads created in step 4 finish?

+1  A: 

You can pass an array of the Tasks you're waiting on to TaskFactory.ContinueWhenAll, along with the new task to start after they're all done.

edit: Possible workaround for your dynamically generated tasks problem: have a two-step continuation; every "dependent task" you start should have a chained ContinueWith which checks the total number of tasks still running, and if it's zero, launches the actual continuation task. That way, every task will do the check when it's done, but only the last one will launch the next phase. You'll need to synchronize access to the "remaining tasks" counter, of course.

tzaman
Does the ContunueWhenAll array get captured at the time of the block or can more be added to the array after the call has occurred?
Scott Chamberlain
Hmm, interesting question. Actually, I'm pretty sure it gets frozen at the time of the call, so it might not be appropriate for your situation.
tzaman