From a logical point of view an application may need dozens or hundreds of threads, some of which will we sleeping most of the time, but a very few will be always running concurrently. The question is: Does it make any sense to spawn more concurrent threads than processors there are in a system, or it is a waste?
从逻辑的角度来看,一个应用程序可能需要几十个或几百个线程,其中一些线程我们将在大多数时间内休眠,但很少有线程将始终同时运行。问题是:产生比系统中的处理器更多的并发线程是否有意义,还是浪费?
I've seen some server applications that implement a scheduler to logically manage tasks (often called jobs), but also spawning a lot of threads, so I don't see where the benefit is.
我已经看到一些服务器应用程序实现了一个调度程序来逻辑地管理任务(通常称为作业),但也产生了很多线程,因此我没有看到它的好处。
Thanks in advance.
提前致谢。
9 个解决方案
#1
Sure. If your software makes frequent use of disk or network IO, you can often improve throughput by adding a few more threads. Those extra threads will be awake and doing stuff while the other threads are blocking on IO.
当然。如果您的软件经常使用磁盘或网络IO,您通常可以通过添加更多线程来提高吞吐量。当其他线程在IO上阻塞时,那些额外的线程将会清醒并执行操作。
#2
Other have talked about situations in which it almost certainly does make sense (when you are doing any kind of slow IO).
其他人已经谈到了几乎肯定有意义的情况(当你做任何缓慢的IO时)。
It might not be a good idea if:
如果出现以下情况可能不是一个好主意:
- your threads are doing CPU bound work
你的线程正在进行CPU绑定工作
and
- the threads each want to use a lot (i.e. significant compared to the cache size) of memory that does not overlap
每个线程都想要使用不重叠的存储器(即与高速缓存大小相比显着)
In this case there is the possibility of causing unnecessary cache misses.
在这种情况下,可能会导致不必要的缓存未命中。
#3
This may make sense if
这可能是有道理的
-
your program design benefits, in that you have parallel tasks that are best implemented in threads, or
您的程序设计的好处,因为您拥有最好在线程中实现的并行任务,或者
-
some of your threads are I/O-bound, so they do not utilize the processors /cores on their own.
您的一些线程受I / O限制,因此它们不会自行使用处理器/内核。
#4
Short answer is "yes".
简短的回答是“是”。
Even thought you could gain more from multithreading on a multiprocessor environement, it's still a useful technology on a single processor machine, mainly because it means you'll delegate some work to the process scheduler, who should have much better information than you have.
即使你认为你可以从多处理器环境中的多线程获得更多,它仍然是单处理器机器上的一项有用技术,主要是因为它意味着你将一些工作委托给流程调度程序,谁应该拥有比你更好的信息。
If you don't multithread, you'll end up doing the scheduling job yourself, which could be a good thing if that's what you need, but will most probably be both tedious and inefficient
如果你没有多线程,你最终会自己完成调度工作,如果这是你需要的,这可能是一件好事,但很可能既乏味又低效
#5
Everytime you have a task waiting for an I/O operation it does make a sense to enclose it in a thread and fire it up. There's a great probability that your thread will be suspended while waiting for the I/O operation to finish. When it gets waken up the result will be waiting for it.
每当你有一个等待I / O操作的任务时,将它封装在一个线程中并将其激活是有意义的。在等待I / O操作完成时,您的线程很可能会被挂起。当它被唤醒时,结果将等待它。
#6
One of the benefits is when you upgrade your hardware, which will likely get more processors/cores.
其中一个好处是当您升级硬件时,可能会获得更多处理器/核心。
#7
Because all modern OS are multi tasking: each thread gets a time share from the processor. It is not actually concurrent execution but given the processor can handle thousands of requests per sec, it is an "apparent" concurrent execution.
因为所有现代操作系统都是多任务处理:每个线程从处理器获得时间共享。它实际上不是并发执行,但考虑到处理器每秒可处理数千个请求,它是一个“明显的”并发执行。
So yes, if the case needs, it does make sense to multi-thread on a single processor.
所以,是的,如果需要,在单个处理器上使用多线程确实有意义。
#8
I found that when writing data parsers which handle larger sets of data over a network it is best to create a thread for each letter of the alphabet (pertaining to the data) and get the program to be more CPU and memory bound. The I/O boundedness inherit with network and disk operations is a major bottleneck so you may as well "get started" on the other data files instead of doing the work sequentially.
我发现在编写通过网络处理更大数据集的数据解析器时,最好为字母表中的每个字母创建一个线程(与数据有关),并使程序更多地受CPU和内存限制。 I / O有界继承网络和磁盘操作是一个主要的瓶颈,所以你也可以“开始”其他数据文件,而不是顺序完成工作。
On a quad core, it would certainly make sense to start more than four threads. It is unlikely that those 4 threads would be spread across more than one of the cores, especially with todays processor speeds.
在四核上,启动超过四个线程肯定是有意义的。这四个线程不太可能分布在多个核心上,尤其是当今的处理器速度。
#9
According to Herb Sutter (one of the leading experts on concurrency), one of the Pillars of Concurrency is Responsiveness and Isolation Via Asynchronous Agents. The summary is:
根据Herb Sutter(并发领域的领先专家之一)的说法,并发的支柱之一是通过异步代理的响应和隔离。摘要是:
Stay responsive by running tasks independently and tasks asynchronously, communicating via messages.
通过独立运行任务和异步执行任务,通过消息进行通信来保持响应。
Great article (and the series as a whole!). I am still waiting for the book.
伟大的文章(和整个系列!)。我还在等这本书。
#1
Sure. If your software makes frequent use of disk or network IO, you can often improve throughput by adding a few more threads. Those extra threads will be awake and doing stuff while the other threads are blocking on IO.
当然。如果您的软件经常使用磁盘或网络IO,您通常可以通过添加更多线程来提高吞吐量。当其他线程在IO上阻塞时,那些额外的线程将会清醒并执行操作。
#2
Other have talked about situations in which it almost certainly does make sense (when you are doing any kind of slow IO).
其他人已经谈到了几乎肯定有意义的情况(当你做任何缓慢的IO时)。
It might not be a good idea if:
如果出现以下情况可能不是一个好主意:
- your threads are doing CPU bound work
你的线程正在进行CPU绑定工作
and
- the threads each want to use a lot (i.e. significant compared to the cache size) of memory that does not overlap
每个线程都想要使用不重叠的存储器(即与高速缓存大小相比显着)
In this case there is the possibility of causing unnecessary cache misses.
在这种情况下,可能会导致不必要的缓存未命中。
#3
This may make sense if
这可能是有道理的
-
your program design benefits, in that you have parallel tasks that are best implemented in threads, or
您的程序设计的好处,因为您拥有最好在线程中实现的并行任务,或者
-
some of your threads are I/O-bound, so they do not utilize the processors /cores on their own.
您的一些线程受I / O限制,因此它们不会自行使用处理器/内核。
#4
Short answer is "yes".
简短的回答是“是”。
Even thought you could gain more from multithreading on a multiprocessor environement, it's still a useful technology on a single processor machine, mainly because it means you'll delegate some work to the process scheduler, who should have much better information than you have.
即使你认为你可以从多处理器环境中的多线程获得更多,它仍然是单处理器机器上的一项有用技术,主要是因为它意味着你将一些工作委托给流程调度程序,谁应该拥有比你更好的信息。
If you don't multithread, you'll end up doing the scheduling job yourself, which could be a good thing if that's what you need, but will most probably be both tedious and inefficient
如果你没有多线程,你最终会自己完成调度工作,如果这是你需要的,这可能是一件好事,但很可能既乏味又低效
#5
Everytime you have a task waiting for an I/O operation it does make a sense to enclose it in a thread and fire it up. There's a great probability that your thread will be suspended while waiting for the I/O operation to finish. When it gets waken up the result will be waiting for it.
每当你有一个等待I / O操作的任务时,将它封装在一个线程中并将其激活是有意义的。在等待I / O操作完成时,您的线程很可能会被挂起。当它被唤醒时,结果将等待它。
#6
One of the benefits is when you upgrade your hardware, which will likely get more processors/cores.
其中一个好处是当您升级硬件时,可能会获得更多处理器/核心。
#7
Because all modern OS are multi tasking: each thread gets a time share from the processor. It is not actually concurrent execution but given the processor can handle thousands of requests per sec, it is an "apparent" concurrent execution.
因为所有现代操作系统都是多任务处理:每个线程从处理器获得时间共享。它实际上不是并发执行,但考虑到处理器每秒可处理数千个请求,它是一个“明显的”并发执行。
So yes, if the case needs, it does make sense to multi-thread on a single processor.
所以,是的,如果需要,在单个处理器上使用多线程确实有意义。
#8
I found that when writing data parsers which handle larger sets of data over a network it is best to create a thread for each letter of the alphabet (pertaining to the data) and get the program to be more CPU and memory bound. The I/O boundedness inherit with network and disk operations is a major bottleneck so you may as well "get started" on the other data files instead of doing the work sequentially.
我发现在编写通过网络处理更大数据集的数据解析器时,最好为字母表中的每个字母创建一个线程(与数据有关),并使程序更多地受CPU和内存限制。 I / O有界继承网络和磁盘操作是一个主要的瓶颈,所以你也可以“开始”其他数据文件,而不是顺序完成工作。
On a quad core, it would certainly make sense to start more than four threads. It is unlikely that those 4 threads would be spread across more than one of the cores, especially with todays processor speeds.
在四核上,启动超过四个线程肯定是有意义的。这四个线程不太可能分布在多个核心上,尤其是当今的处理器速度。
#9
According to Herb Sutter (one of the leading experts on concurrency), one of the Pillars of Concurrency is Responsiveness and Isolation Via Asynchronous Agents. The summary is:
根据Herb Sutter(并发领域的领先专家之一)的说法,并发的支柱之一是通过异步代理的响应和隔离。摘要是:
Stay responsive by running tasks independently and tasks asynchronously, communicating via messages.
通过独立运行任务和异步执行任务,通过消息进行通信来保持响应。
Great article (and the series as a whole!). I am still waiting for the book.
伟大的文章(和整个系列!)。我还在等这本书。