Can I configure the number of threads used by Parallel Extensions?

淺唱寂寞╮ 提交于 2019-12-11 02:48:14

问题


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.


回答1:


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. :)




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/1897196/can-i-configure-the-number-of-threads-used-by-parallel-extensions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!