开发者

Query in Task Parallel Library implementation

HI ALL,

My query is regarding TaskParallel Library implementations.

I have a List with me. I need to execute all the tasks in parallel and at any point of time the number of tasks that are being executed should be 3. i.e, if i have 9 tasks the first 3 tasks should be started initially, and if any 1 is completed the next one should be started and 开发者_StackOverflowat any point of time not more than 3 tasks should be running.

What is the best way to implement this?

Thanks!


If you're using Parallel::ForEach you can use an overload that takes a ParallelOptions on which you would set MaxDegreeOfParallelism = 3. If you're using PLINQ you can use WithDegreeOfParallelism(3).

Keep in mind this only limits the maximum number of threads that might be executing your work at any given time, it does/can not guarantee that all three threads will necessarily be able to start/run at once.


Did you already look into System.Threading? There is a Parallel class, which seems like what you are looking for.


You could implement a scheduler that limits the degree of parallelism.

http://msdn.microsoft.com/en-us/library/ee789351.aspx

I'm unclear as to your scenario but this approach can be used to limit the maximum number of tasks being executed but may not guarentee that three run all the time, simply that no more than three can run at once.

Another approach might be to start three Tasks and have them read data from a ConcurrentQueue containing the information needed to execute the actual work. This would allow you to always have three things executing in parallel.

Essentially what you're doing is somewhat at odds with the philosophy behind the TPL which is really about expressing your work as potential parallelism and having the runtime schedule this on as many cores as are available.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜