0

I would like to know if there is an existent framework which can manage some "tasks".

I'm aware of Task Parallelism, but (correct me if I'm wrong) I don't think it fits my needs (mostly because it works on a finished list of tasks).

The base of my needs is that I want to give to X threads some work to do.

But:

  • Every task can generate create new tasks to be executed in the future(can be 0 task like it can be 100 tasks
  • If my task list is empty, I would like that my non-working thread wait that all thread have finished their task, maybe I've one last thread running, which will generate 50 new tasks that other 3 threads can handle.

Do you know something which can helps me? Or should I have to manage everything by hands?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
J4N
  • 19,480
  • 39
  • 187
  • 340

2 Answers2

6

TPL would work by the sound of it. You can create subtasks, and you can use WaitAll to make sure all tasks are completed.

This way you don't have to worry about thread management, either.

Without knowing much more about your code, it seems like it could be something like this pseudocode:

//for loop to create tasks
var task = new Task(()=>Task.Factory.StartNew(()=>dostuff));
taskList.Add(task)
//end for loop

Task.WaitAll(taskList);

UPDATE:

I also forgot that the subtasks need to be marked at inner tasks or else the outer task will not wait. Here is an MSDN on nested tasks and how to work with them. But, in a nutshell, when you create a nested task you have to mark it as attached to the parent task.

TaskCreationOptions.AttachedToParent
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180
  • 1
    Just be aware that `WaitAll()` blocks the current thread. And if you do that a lot, it could cause having a lot of threads that don't do any useful work. In C# 5, there is an easy fix: `await Task.WhenAll(taskList)`. – svick Jul 02 '12 at 13:42
  • Ok, but I've a problem:WaitAll wait only on listed thread, but if I create children thread, the main thread don't wait on them. I know that I can attach them to the parent, but I don't want because I don't need the response. Can we specify somewhere the maximum of task to run in parallel? – J4N Jul 02 '12 at 13:43
  • @svick Agreed, however you should also be aware that if you go to use any values from that `await` at a top level method, then `await` can still block the main thread. I am really nervous about a wave of possibly odd async problems coming from programmers misunderstanding/misusing `async` and `await`. At the base level, @svick is right and I agree, I am just noting to make sure any dev understands the mechanics of async/await before throwing them around without thought :) – Justin Pihony Jul 02 '12 at 13:50
  • Here is what I did to test it: http://pastebin.com/KA7qQC21 Any idea how to make that the "Run" call returns only when all sub thread have finished their jobs? And is there a way to know how many task have still to be done(something like a progress information? – J4N Jul 02 '12 at 14:03
  • @J4N I just updated my answer. Sorry, it has been a bit since I worked with the TPL and forgot a part – Justin Pihony Jul 02 '12 at 14:12
  • @JustinPihony `await` by itself will never block. If you `Wait()` on the returned `Task` or directly access its `Result`, that will of course block (and in some cases, cause a deadlock). But if you use `await` consistently everywhere, there will be no blocking. – svick Jul 02 '12 at 14:20
  • @svick Yes, but you know that. I fear devs will not really understand await and not pay attention to this...or even worse, may put intensive work after an await, thus causing a delayed deadlock which would REALLY confuse end users....Maybe I am being too paranoid :) – Justin Pihony Jul 02 '12 at 14:26
  • I don't want to create AttachedParent, because in my case, it will result by having 10-15 process working, and 500 waiting on their childs to finish. maybe the solution is to use a simple MUTEX, with a +1 when I start a new thread, and a -1 when the thread end? – J4N Jul 02 '12 at 14:36
  • 1
    No, TPL can handle that also: http://stackoverflow.com/questions/2898609/system-threading-tasks-limit-the-number-of-concurrent-tasks You just set `MaxDegreesOfParallelism` – Justin Pihony Jul 02 '12 at 14:43
1

Take a look at Actor Framework which is well suited for distributed parallel task processing, there is a list of them and here is one of them on .NET