简体   繁体   English

TPL要求更高的并行度

[英]TPL force higher parallelism

When queuing Tasks to the ThreadPool , the code relies on the default TaskScheduler to execute them. 当排队Tasks线程池 ,代码依赖于默认TaskScheduler执行它们。 In my code example, I can see that 7 Tasks maximum get executed in parallel on separate threads. 在我的代码示例中,我可以看到在单独的线程上最多并行执行7个Tasks

new Thread(() =>
{
    while (true)
    {
        ThreadPool.GetAvailableThreads(out var wt, out var cpt);
        Console.WriteLine($"WT:{wt} CPT:{cpt}");
        Thread.Sleep(500);
    }
}).Start();

var stopwatch = new Stopwatch();
stopwatch.Start();
var tasks = Enumerable.Range(0, 100).Select(async i => { await Task.Yield(); Thread.Sleep(10000); }).ToArray();
Task.WaitAll(tasks);
Console.WriteLine(stopwatch.Elapsed.TotalSeconds);
Console.ReadKey();

Is there a way to force the scheduler to fire up more Tasks on other threads? 有没有一种方法可以强制调度程序在其他线程上启动更多Tasks Or is there a more "generous" scheduler in the framework without implementing a custom one? 还是在框架中有一个更“慷慨的”调度程序而不实现自定义调度程序?

EDIT: 编辑:

Adding ThreadPool.SetMinThreads(100, X) seems to do the trick, I presume awaiting frees up the thread so the pool think it can fire up another one and then it immediately resumes. 添加ThreadPool.SetMinThreads(100, X)似乎可以解决问题,我想等待释放线程,以便池认为它可以启动另一个线程,然后立即恢复。

By default, the minimum number of threads is set to the number of processors on a system. 默认情况下,最小线程数设置为系统上的处理器数。 You can use the SetMinThreads method to increase the minimum number ofthreads. 您可以使用SetMinThreads方法来增加最小线程数。 However, unnecessarily increasing these values can cause performance problems. 但是,不必要地增加这些值可能会导致性能问题。 If too many tasks start at the same time, all of them might appear to be slow. 如果太多任务同时开始,则它们似乎都变慢了。 In most cases, the thread pool will perform better with its own algorithm for allocating threads. 在大多数情况下,线程池使用自己的分配线程算法会更好地执行。 Reducing the minimum to less than the number of processors can also hurt performance. 将最小值减少到少于处理器数量也会损害性能。

From here: https://msdn.microsoft.com/en-us/library/system.threading.threadpool.setminthreads(v=vs.110).aspx 从这里: https : //msdn.microsoft.com/zh-cn/library/system.threading.threadpool.setminthreads(v=vs.110).aspx

I removed AsParallel as it is not relevant and it just seems to confuse readers. 我删除了AsParallel因为它AsParallel ,这似乎使读者感到困惑。

Is there a way to force the scheduler to fire up more Tasks on other threads? 有没有一种方法可以强制调度程序在其他线程上启动更多任务?

You cannot have more executing threads than you have CPU cores. 执行线程的数量不能超过CPU内核的数量。 This is just how computers work. 这就是计算机的工作方式。 If you use more threads, then your work will actually get done more slowly since the threads must swap in and out of the cores in order to run. 如果您使用更多的线程,那么您的工作实际上将更慢地完成,因为线程必须交换进出内核才能运行。

Or is there a more "generous" scheduler in the framework without implementing a custom one? 还是在框架中有一个更“慷慨的”调度程序而不实现自定义调度程序?

PLINQ is already tuned to make maximum use of the hardware. 已经对PLINQ进行了调整,以充分利用硬件。

You can see this for yourself if you replace the Thread.Sleep call with something that actually uses the CPU (eg, while (true) ; ), and then watch your CPU usage in Task Manager. 如果将Thread.Sleep调用替换为实际使用CPU的东西(例如while (true) ; ),然后在任务管理器中查看CPU的使用情况,则可以自己查看。 My expectation is that the 7 or 8 threads used by PLINQ in this example is all your machine can handle. 我的期望是,本示例中PLINQ使用的7或8个线程是您的计算机可以处理的全部。

Useful link that explains it can be done with ThreadPool.SetMinThread : 可以使用ThreadPool.SetMinThread进行解释的有用链接:

https://gist.github.com/JonCole/e65411214030f0d823cb#file-threadpool-md https://gist.github.com/JonCole/e65411214030f0d823cb#file-threadpool-md

Try this: https://msdn.microsoft.com/en-us/library/system.threading.threadpool.setmaxthreads(v=vs.110).aspx 试试这个: https : //msdn.microsoft.com/zh-cn/library/system.threading.threadpool.setmaxthreads(v=vs.110).aspx

You can set the number of worker threads (first argument). 您可以设置工作线程的数量(第一个参数)。

使用WithDegreeOfParallelism扩展:

Enumerable.Range(0, 100).AsParallel().WithDegreeOfParallelism(x).Select(...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM