对于特定处理器,同时运行的线程的更好数目是多少? [英] What is the better number of threads running simultaneous for specific processor?

查看:226
本文介绍了对于特定处理器,同时运行的线程的更好数目是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台带有8核处理器的计算机,并且我对我的计算机需要使用8核处理器的最大线程数(软件,不是处理器的线程数)有疑问.核.

I have a computer with an 8-core processor, and I have a doubt about what's the max number of threads (software, don't thread of processor.) that my computer needs to use the max potencial of your 8-core.

我正在同时创建160个线程,所以处理器的每个内核将处理大约20个线程,对吗?

I'm creating 160 threads simultaneous, so each core of my processor will process about ~20 threads, is it correct?

我的问题是:

  • 处理器核心20个线程是个好数字吗?
  • 与运行的线程数无关,是否将其除以内核数?
  • 如何根据我的处理器知道更好的线程数?

推荐答案

我正在同时创建160个线程,所以处理器的每个内核将处理大约20个线程,对吗?

I'm creating 160 threads simultaneous, so each core of my processor will process about ~20 threads, is it correct?

不完全是.每个内核一次只能处理一个线程.当操作系统确定线程具有足够的关注度时,它将切换到另一个线程.如果您可以在8个线程中完成相同的工作,那么您将拥有相同的吞吐量以及避免不必要的上下文切换而节省的成本.

Not quite. Each core can only process a single thread at a time. When the OS decides the thread had enough of the spotlight, it will switch it out for another thread. If you can do the same work in 8 threads, you're going to have the same throughput plus the savings you get from avoiding unnecessary context switching.

处理器核心20个线程是个好数字吗?

20 threads by core of processor is a good number?

不.每个核心一个线程是一个地方.超线程核心大约占两个-工作原理各不相同.

No. One thread per core is the spot. Hyperthreaded cores count for two, approximately - how well that works varies.

与运行的线程数无关,是否将其除以内核数?

Independent the number of threads running, its will be divided equality by the number of cores?

不.这是由操作系统决定的,它将在其决策过程中采取许多不同的措施.同样,被阻塞的线程将不会在任何地方执行.实际上,其中一些线程可能比其他线程占用更多的CPU时间.

No. This is decided by the operating system, which will tend to take many different things in its decision-making. Also, threads which are blocking will not execute anywhere. It's actually quite possible for a few of those threads to hog much more CPU time than others.

如何根据我的处理器知道更好的线程数?

How to know whats the better number of threads according with my processor?

使用比线程更高级别的构造.这意味着将线程池用于简单的工作项,将Parallel.For和family用于同步"并行处理,将异步Task用于复杂的异步工作任务(包括线程池片段).确实没有太多理由再手动创建线程-除非您真的知道自己在做什么,以及在专用线程上拥有线程的重要性.

Use higher level constructs than threads. That means using the thread pool for simple work items, Parallel.For and family for "synchronous" paralellism, and asynchronous Tasks for complex asynchronous work tasks (including thread pool fragments). There really aren't many good reasons to manually create threads anymore - unless you really know what you're doing, and why it matters to have it on a dedicated thread.

关键点是这仅适用于CPU工作.您可以轻松地使一个线程同时处理一百个单独的异步任务.当针对异步API使用await时,这特别容易使用.只要CPU真正起作用,每个CPU的单线程即可轻松获得100%的利用率.如果不是这样,则无论如何您都想使用异步I/O-在等待时浪费线程(与线程在内存中的成本,切换开销,调度程序超负荷工作,垃圾回收...)无关. em>.

The critical point is this only applies to CPU work. You can easily have a single thread handling a hundred separate asynchronous tasks at the same time. This is especially easy to work with when using await against asynchronous APIs. Single thread per CPU can easily get you 100% utilization as long as it actually does CPU work. If it doesn't, you want to use asynchronous I/O anyway - there's no point in wasting threads (accompanied with their cost in memory, switching overhead, overtasking the scheduler, garbage collection...) while waiting.

典型示例是处理某些数据库工作的异步Web服务:

The typical example would be an asynchronous web service dealing with some database work:

string GetUserName(Guid userId)
{
  return Db.GetUser(userId).Name;
}

此同步方法将在处理数据库请求时占用请求线程.如果您的数据库请求花费了一秒钟,并且您同时有2000个请求,那么您将需要2000个线程来处理这些请求.但是,数据库请求只是异步I/O-线程基本上正在等待数据库的响应返回.线程浪费了等待的时间.

This synchronous method will take up the request thread while processing the DB request. If your DB requests takes a second, and you have 2000 requests at the same time, you'll need 2000 threads handling the requests. However, DB requests are just asynchronous I/O - the thread is basically waiting for the response from the DB to come back. The thread is wasted waiting.

相反,您可以使用异步API:

Instead, you could use an asynchronous API:

async Task<string> GetUserName(Guid userId)
{
  var user = await Db.GetUserAsync(userId);

  return user.Name;
}

代码几乎相同,但是await构造实际上并没有阻塞请求线程-它基本上说:好,我完成了,您可以将这个线程用于其他用途.当我准备继续的时候.".这种模式意味着您永远都不需要比CPU内核更多的线程-如果CPU处于100%,则没有必要添加更多线程来处理负载.如果不是,则表示某个线程没有执行任何CPU工作-很好,它将在发生另一项工作时使用.现在,不再有2000个线程来处理2000个请求,而是有8个线程来处理相同 2000个请求.

The code is almost identical, but the await construct doesn't actually block the request thread - it basically says "Okay, I'm done, you can use this thread for something else. I'll ping you back when I'm ready to continue.". This patterns means that you never need more threads than CPU cores - if the CPU is at 100%, there's no point in adding more threads to handle the load. If it's not, it means some thread isn't doing any CPU work - which is fine, it will be used when another piece of work comes around. And now, instead of having 2000 threads handling 2000 requests, you have 8 threads handling the same 2000 requests.

这篇关于对于特定处理器,同时运行的线程的更好数目是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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