根据处理器/内核创建线程 [英] Creating Threads based on processors/cores

查看:93
本文介绍了根据处理器/内核创建线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于要创建的最佳线程数,我不清楚整个线程过程如何工作.我听说过/应该只创建与我的计算机正在运行的处理器数量相对应的多个线程.示例:四核处理器有4个线程,双核处理器有2个线程,等等...

那么,如果我正在运行四核计算机并创建10个线程来处理一些非常密集的计算和数据库操作,会发生什么?

我之所以这样问,是因为我实际上正在运行四核计算机,并实时进行大量的数学计算和数据库操作(插入和更新),而我的应用程序有效运行的唯一方法是使用10个线程.如果我执行任何其他操作,性能都会下降.

有人可以告诉我发生了什么事吗?

预先感谢,

-Donald

I''m not clear on how the whole threading process works with regards to the optimal number of threads to create. I''ve heard/read that I should only create a number of threads that corresponds to the number of processors my machine is running. Example: 4 threads for a quad core processor, 2 threads for a dual core processor, etc...

So what happens if I''m running a quad core machine and I create 10 threads to handle some really intensive calculations and database operations?

The reason I ask this is because I''m actually running a quad core machine and doing intensive math calcs and database operations (inserts and updates) in realTime and the only way that my app runs efficiently is with 10 threads. If I run anything less performance degrades.

Can someone please clarify to me what''s going on?

Thanks in advance,

-Donald

推荐答案

可行的运行线程的数量实际上与您拥有多少内核无关,因为.Net使用了合适的可用内核.这也很大程度上取决于线程在做什么,以及它们是处于等待状态还是正在主动处理某些东西.

某个地方有一个收支平衡点,但是我不确定是否有任何可靠的方法可以预测它,而没有让应用程序监视它自己的整体性能(当然,可以在它的自己的线程-咧嘴笑),并根据soe硬编码阈值调整并发运行的最大线程数.

如果在您的环境中十是个好数字,那么十是一个好数字.

.Net 4引入了并行任务,这些任务可能(或可能没有)更有效.
The number of viable runnning threads really has nothing to do with how many cores you have since .Net uses the available cores as it sees fit. It also depends pretty much on what the threads are doing, and whether they''re in a waiting state or actively processing something.

There is a break-even point somewhere, but I''m not sure if there''s any reliable way to predict it short of having the app monitor it''s own overall performance (of course, that would be handled in its own thread - grin), and adjust the maximum number of concurrently running threads on based on soe hard-coded threshold.

If ten is a good number in your environment, then ten is a good number.

.Net 4 introduced parallel tasks, which may (or may not) be more efficient.


除了约翰所说的:您可以控制某些线程与某些内核的亲和力:使用System.Threading.Thread.BeginThreadAffinitySystem.Threading.Thread.EndThreadAffinity.在过程级别,您可以使用System.Diagnostics.Process.ProcessorAffinity.

不建议在正常情况下这样做,这通常是毫无意义的.我看到这种花招的原因之一是提供了一些坏"硬件,需要定期进行大量轮询.它将使其他内核/CPU的负载更加可预测.

—SA
In addition to what John said: You can control of the affinity of some threads to some cores: use System.Threading.Thread.BeginThreadAffinity, System.Threading.Thread.EndThreadAffinity. At the level of the process, you can use System.Diagnostics.Process.ProcessorAffinity.

Doing it a normal situation is not recommended and would be usually pointless. One reason for such trick I can see is serving some "bad" hardware requiring heavy regular polling; it will make the load of other cores/CPUs more predictable.

—SA


在您的情况下,通常的方法是为每个内核创建一个计算线程,将IO卸载到单独的线程中-可能使用单独的计算机进行登台(包括存储)传入的数据,并类似地尝试将结果存储转移到单独的计算机上-通常使用消息查询解决方案.

In your case the usual approach is to create one calculation thread for each core, offload IO to separate threads - possibly using separate computers for staging (including storing) of incomming data, and similarly try to offload storing of results to separate computers - often using a message queing solution.

Staging computer 1 ---
                        \
Staging computer 2 ---   \
                           calculating computer enques results to ---> storage solution  
Staging computer 3 ---   /

Staging computer 4 --- /



当您要进行大量计算时,将IO移开通常是唯一真正的解决方案.与将结果放入消息队列解决方案相比,数据库IO的价格要高得多,特别是如果您使用的是以智能方式处理负载平衡的东西.

问候
Espen Harlinn



Moving IO out of the way is often the only real solution when you''re doing heavy calculations. Database IO is *much* more expensive then enquing results to a message quing solution, especially if you''re using something that handles load balancing in an intelligent way.

Regards
Espen Harlinn


这篇关于根据处理器/内核创建线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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