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
- Parent thread creates somewhere between 1 and (NUMBER_OF_CPU_CORES – 1) tasks.
- Parent thread creates task to be run when all of the worker tasks are finished.
- Parent thread creates a monitoring thread
- 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?
You can pass an array of the
Tasks you’re waiting on toTaskFactory.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
ContinueWithwhich 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.