是否可以限制 Parallel.ForEach 的内核? [英] Is it possible to limit the cores for Parallel.ForEach?

查看:17
本文介绍了是否可以限制 Parallel.ForEach 的内核?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的代码中使用了 Parallel.ForEach.我所有的 8 个内核都达到 100%.这对服务器上运行的其他应用程序不利.是否可以将执行限制为 4 个内核?

I'm using a Parallel.ForEach in my code. All my 8 cores go to 100%. This is bad for the other apps that are running on the server. Is it possible to limit execution to like 4 cores?

推荐答案

ParallelOptions 的实例传递给 ParallelOptions.MaxDegreeOfParallelism 设置为 4 到 Parallel.ForEach.

Pass an instance of ParallelOptions with ParallelOptions.MaxDegreeOfParallelism set to 4 to Parallel.ForEach.

尽管如此,这在其他机器上可能没有意义,因为它们的内核可能比您多或少.一般来说,你应该让框架决定并行度.

Nevertheless this might not make sense on other machines, that might have more or less cores than you. In general you should let the framework decide the degree of parallelism.

这篇关于是否可以限制 Parallel.ForEach 的内核?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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