开发者

parallel programming in TPL

开发者 https://www.devze.com 2023-01-28 06:56 出处:网络
im using tasks parallel library in .net 4.0 i want to have the ability to specify a task for each core independant of the other cores. usually in TPL, i create a task and i tell it to run in parallel

im using tasks parallel library in .net 4.0 i want to have the ability to specify a task for each core independant of the other cores. usually in TPL, i create a task and i tell it to run in parallel, i have no contr开发者_如何学Pythonol over the number of threads created nor can i control the number of cores to participate in the parallel task. also, i cant specify each particular core a different task. i'd like to know how to achieve this in TPL if it is possible.


As others have pointed out, you probably don't want to do this.

There is a ParallelExtensionsExtras library on CodePlex that has various task schedulers, one of which is a "thread per task" scheduler, and another (LimitedConcurrencyLevelTaskScheduler) that allows you to specify the degree of parallelism.

However, they don't provide thread affinity to a particular core. You'd have to code this on your own.


The TPL scales the degree of concurrency dynamically to most efficiently use all the processors that are available. If this is a problem, TPL may not be for you.

The closest thing I'm aware of is ParallelEnumerable.WithDegreesOfParallelism, which sets the the maximum number of concurrently executing tasks that will be used to process the query.

It doesn't sound like that fits your bill, however, so perhaps you need fine-grained control, in which case I'd recommend using threads directly.


i have no control over the number of threads created nor can i control the number of cores to participate in the parallel task.

By design, the purpose of the TPL is that you don't have to deal with those issues. Tuning can be very complicated, the TPL does a pretty good job.


I just noticed that there is a way to control the degree of parallelism in TPL. Example code is as below, which uses ParallelOptions to decide the max concurrency in parallel execution of a loop.

This is taken from an article by Richard Carr at: Control Degree of Parallelism in TPL

ParallelOptions po = new ParallelOptions();
po.MaxDegreeOfParallelism = 2;

Parallel.For(0, 20, po, i =>
{
    Console.WriteLine("{0} on Task {1}", i, Task.CurrentId);
});
0

精彩评论

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

关注公众号