在执行Parallel.ForEach期间是否可以更改parallelOptions.MaxDegreeOfParallelism [英] Is it possible to change parallelOptions.MaxDegreeOfParallelism during execution of a Parallel.ForEach

查看:332
本文介绍了在执行Parallel.ForEach期间是否可以更改parallelOptions.MaxDegreeOfParallelism的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行多线程循环:

I am running a multi-threaded loop:

protected ParallelOptions parallelOptions = new ParallelOptions();

parallelOptions.MaxDegreeOfParallelism = 2;
Parallel.ForEach(items, parallelOptions, item =>
{
// Loop code here
});

我想在并行循环执行期间更改parallelOptions.MaxDegreeOfParallelism以减少或增加线程数.

I want to change the parallelOptions.MaxDegreeOfParallelism during the execution of the parallel loop to reduce or increase a number of threads.

parallelOptions.MaxDegreeOfParallelism = 5;

似乎并没有增加线程.有人有什么想法吗?

It doesn't seem to increase the threads. Does anyone have any ideas?

推荐答案

甚至试图这样做的问题是,这是一个难题.对于初学者,您如何甚至可靠地观察CPU和磁盘利用率?不频繁地对CPU进行采样将无法很好地了解实际情况,而对磁盘利用率的采样则更加困难.其次,您的任务的粒度是多少,您实际上可以多久才能真正更改正在运行的数量.第三,事情会随着时间的推移而迅速变化,因此您需要对观察结果进行某种过滤.第四,理想的线程数将取决于代码实际在其上运行的CPU.第五,如果分配过多的线程,那么它们之间就会发生混乱,而不是做有用的工作.

The issue with even trying to do this is that it's a hard problem. For starters, how do you even observe CPU and disk utilization reliably? Sampling CPU infrequently will give a poor picture of what's actually going on and sampling disk utilization is even harder. Secondly, what is the granularity of your tasks and how often can you quickly can you actually change the number that are running. Thirdly things change rapidly over time so you need to apply some kind of filtering to your observations. Fourthly, the ideal number of threads will depend on the CPU that the code is actually running on. Fifthly, if you allocate too many threads you'll be thrashing between them instead of doing useful work.

请参见 http://msdn.microsoft.com/en-us/杂志/ff960958.aspx 讨论了.NET中的线程池如何处理决定使用多少线程的复杂任务.

See http://msdn.microsoft.com/en-us/magazine/ff960958.aspx for a discussion on how the Thread Pool in .NET handles the complex task of deciding how many threads to use.

您还可以使用反射器,看看TPL用于分配线程并避免不必要的上下文切换的代码-这很复杂,甚至都没有考虑磁盘访问!

You could also use reflector and take a look at the code that TPL uses to allocate threads and to avoid unnecessary context switching - it's complex and that's not even taking disk access into account!

您可以尝试在较低优先级的线程上执行任务(创建自己的TaskScheduler来运行优先级低于正常值的线程实际上很容易).至少这将确保您可以在不影响系统其余部分的情况下运行100%的CPU.处理线程优先级本身就充满了问题,但是如果这纯粹是后台任务,它可能会很简单并且可能会有所帮助.

You could instead try executing the tasks on a lower priority thread (creating your own TaskScheduler that runs threads with a priority of below-normal is actually quite easy). That at least will ensure that you can run up 100% CPU without impacting the rest of the system. Messing with thread priorities is in itself fraught with problems but if this is a purely background task it can be straightforward and might help.

通常,磁盘利用率是导致其他应用程序受一个贪婪的应用程序困扰的真正罪魁祸首. Windows可以轻松地在应用程序之间公平地分配CPU,但是当涉及到相对较慢的磁盘访问时,这是另一回事.与其尝试动态地调整正在运行的线程数,不如直接调整应用程序的速度,以免它过于频繁地访问磁盘.您可以在不更改活动线程数的情况下执行此操作.

Often though, disk utilization is the real culprit when it comes to other applications suffering at the hands of one greedy application. Windows can allocate CPU fairly between applications with ease but when relatively slow disk access is involved it's quite another matter. Instead of trying to dynamically adjust how many thread you have running you may instead need to simply throttle your application such that it doesn't access the disk too often. That's something you can do without changing how many threads are active.

您还可以查看SetPriorityClass作为通知OS的一种方法,该过程不如系统上运行的其他应用程序重要,请参阅

You could also look at SetPriorityClass as a way to inform the OS that your process is less important than other applications running on the system, see How can I/O priority of a process be increased? for more information. But that assumes your whole process is less important, not just this part of it.

这篇关于在执行Parallel.ForEach期间是否可以更改parallelOptions.MaxDegreeOfParallelism的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆