我可以配置并行扩展使用的线程数吗?
我目前使用的并行扩展是 Reactive Extensions for .Net(接收)。我相信它们也可以通过 .Net 4 beta 版本获得。
1)有没有办法确定实际使用了多少线程。我认为这与Environment.ProcessorCount(逻辑核心数量)有关,但我想检查一下。
2)有什么方法可以配置您希望使用多少个线程?我注意到有一个 ParallelOptions.MaxDegreeOfParallelism 属性看起来很有希望,但它似乎默认为 -1 (对线程数没有限制),而且我也不太确定是否可以设置一次对于当前应用程序,而不是传递到对 Parallel.For() 等的每次调用中。
I'm currently using the Parallel Extensions that are part of Reactive Extensions for .Net(Rx). I believe they are also available through .Net 4 beta releases.
1) Is there any way to determine how many threads are actually being utilized. I assume this is related to Environment.ProcessorCount
(# of logical cores) but I want to check this.
2) Is there any way of configuring how many thread you wish to use? I noticed there is a ParallelOptions.MaxDegreeOfParallelism
property that looks promising but it appears to default to -1 (no limit to the number of threads) and I'm also not quite sure if this can be set once for the current application, as opposed to being passed in to each call to Parallel.For()
, etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道 .Net 4.0,但在 PFX June 2008 CTP 中有一个 System.Threading.Tasks.TaskManagerPolicy 类,我用它来限制使用的处理器数量。关于如何使用的例子有很多,google一下就可以了。 :)
I dunno about .Net 4.0, but in PFX June 2008 CTP there is
System.Threading.Tasks.TaskManagerPolicy
class, which I'm using to limit used processors count. There are lots of examples on how to use it, just google it. :)Observable.Context 对象定义了如何在 Rx 线程内完成并行工作。
默认实现使用 global::System.Threading.ThreadPool.QueueUserWorkItem 功能。因此您可以在那里设置 MaxWorkerThreads 来定义最大值。
在 rx 团队博客上阅读更多内容。
http: //blogs.msdn.com/rxteam/archive/2009/11/21/observable-context-observable-subscribeon-and-observable-observeon.aspx
希望这会有所帮助。
There is the Observable.Context object, that defines how parallel stuff is done inside the Rx Threads.
The default implementation uses the global::System.Threading.ThreadPool.QueueUserWorkItem feature. so you can set there the MaxWorkerThreads to define a maximum.
read more on the rx team blog.
http://blogs.msdn.com/rxteam/archive/2009/11/21/observable-context-observable-subscribeon-and-observable-observeon.aspx
hope this helps.
更新:这个选项出现在.Net 4中;
ParallelOptions.MaxDegreeOfParallelism
在回答我自己的问题时,答案似乎很简单 - 否。使用的线程/核心数似乎不可配置。
UPDATE: This option appeared in .Net 4;
ParallelOptions.MaxDegreeOfParallelism
In answer to my own question, the answer appears to simply be - No. The number of threads/cores used doesn't appear to be configurable.