multithreading 什么样的应用程序需要多线程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/455084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 01:00:02  来源:igfitidea点击:

What kinds of applications need to be multi-threaded?

multithreading

提问by Alex Baranosky

What are some concrete examplesof applications that need to be multi-threaded, or don't need to be, but are much better that way?

有哪些应用程序需要多线程或不需要多线程的具体示例,但这样会更好?

Answers would be best if in the form of one application per post that way the most applicable will float to the top.

如果以每个帖子一个应用程序的形式给出答案,那么最适用的将浮到顶部。

采纳答案by Ray Hayes

There is no hard and fast answer, but most of the time you will not see any advantage for systems where the workflow/calculation is sequential. If however the problem can be broken down into tasks that can be run in parallel (or the problem itself is massively parallel [as some mathematics or analytical problems are]), you can see large improvements.

没有硬性和快速的答案,但大多数情况下,对于工作流程/计算是顺序的系统,您不会看到任何优势。然而,如果可以将问题分解为可以并行运行的任务(或者问题本身是大规模并行的 [如某些数学或分析问题]),您就会看到很大的改进。

If your target hardware is single processor/core, you're unlikely to see any improvement with multi-threaded solutions (as there is only one thread at a time run anyway!)

如果您的目标硬件是单处理器/内核,则您不太可能看到多线程解决方案的任何改进(因为无论如何一次只能运行一个线程!)

Writing multi-threaded code is often harder as you may have to invest time in creating thread management logic.

编写多线程代码通常更难,因为您可能需要投入时间来创建线程管理逻辑。

Some examples

一些例子

  • Image processingcan often be done in parallel (e.g. split the image into 4 and do the work in 1/4 of the time) but it depends upon the algorithm being run to see if that makes sense.
  • Rendering of animation(from 3DMax,etc.) is massively parallel as each frame can be rendered independently to others -- meaning that 10's or 100's of computers can be chained together to help out.
  • GUIprogramming often helps to have at least two threads when doing something slow, e.g. processing large number of files - this allows the interface to remain responsive whilst the worker does the hard work (in C# the BackgroundWorker is an example of this)
  • 图像处理通常可以并行完成(例如,将图像分成 4 个并在 1/4 的时间内完成工作),但这取决于正在运行的算法以查看是否有意义。
  • 动画的渲染(来自 3DMax 等)是大规模并行的,因为每一帧都可以独立于其他帧进行渲染——这意味着可以将 10 或 100 台计算机链接在一起以提供帮助。
  • GUI编程通常有助于在做一些缓慢的事情时至少有两个线程,例如处理大量文件 - 这允许界面在工作人员完成艰苦工作时保持响应(在 C# 中,BackgroundWorker 就是这样的一个例子)

GUI's are an interesting area as the "responsiveness" of the interface can be maintained without multi-threading if the worker algorithm keeps the main GUI "alive" by giving it time, in Windows API terms (before .NET, etc) this could be achieved by a primitive loop and no need for threading:

GUI 是一个有趣的领域,因为如果工作算法通过给它时间来保持主 GUI“活着”,则可以在没有多线程的情况下保持界面的“响应性”,在 Windows API 术语中(在 .NET 之前等)这可能是通过原始循环实现,不需要线程:

MSG msg;
while(GetMessage(&msg, hwnd, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);

    // do some stuff here and then release, the loop will come back
    // almost immediately (unless the user has quit)
}

回答by MiniQuark

Serversare typically multi-threaded (web servers, radius servers, email servers, any server): you usually want to be able to handle multiple requests simultaneously. If you do not want to wait for a request to end before you start to handle a new request, then you mainly have two options:

服务器通常是多线程的(Web 服务器、radius 服务器、电子邮件服务器、任何服务器):您通常希望能够同时处理多个请求。如果您不想等待请求结束才开始处理新请求,那么您主要有两种选择:

  1. Run a process with multiple threads
  2. Run multiple processes
  1. 运行多线程进程
  2. 运行多个进程

Launching a process is usually more resource-intensive than lauching a thread (or picking one in a thread-pool), so servers are usually multi-threaded. Moreover, threads can communicate directly since they share the same memory space.

启动一个进程通常比启动一个线程(或在线程池中选择一个线程)消耗更多的资源,因此服务器通常是多线程的。此外,线程可以直接通信,因为它们共享相同的内存空间。

The problem with multiple threads is that they are usually harder to code right than multiple processes.

多线程的问题在于它们通常比多进程更难正确编码。

回答by Tall Jeff

There are really three classes of reasons that multithreading would be applied:

应用多线程实际上有三类原因:

  • Execution Concurrency to improve compute performance: If you have a problem that can be broken down into pieces and you also have more than one execution unit (processor core) available then dispatching the pieces into separate threads is the path to being able to simultaneously use two or more cores at once.
  • Concurrency of CPU and IO Operations: This is similar in thinking to the first one but in this case the objective is to keep the CPU busy AND also IO operations (ie: disk I/O) moving in parallel rather than alternating between them.
  • Program Design and Responsiveness: Many types of programs can take advantage of threading as a program design benefit to make the program more responsive to the user. For example the program can be interacting via the GUI and also doing something in the background.
  • 执行并发以提高计算性能:如果您的问题可以分解为多个部分,并且您还有多个可用的执行单元(处理器核心),那么将这些部分分派到单独的线程中是能够同时使用两个的途径或一次更多核心。
  • CPU 和 IO 操作的并发性:这与第一个想法类似,但在这种情况下,目标是保持 CPU 忙碌并且 IO 操作(即:磁盘 I/O)并行移动而不是在它们之间交替。
  • 程序设计和响应性:许多类型的程序都可以利用线程作为程序设计的优势,使程序对用户的响应能力更强。例如,程序可以通过 GUI 进行交互,也可以在后台执行某些操作。

Concrete Examples:

具体例子:

  • Microsoft Word: Edit document while the background grammar and spell checker works to add all the green and red squiggle underlines.
  • Microsoft Excel: Automatic background recalculations after cell edits
  • Web Browser: Dispatch multiple threads to load each of the several HTML references in parallel during a single page load. Speeds page loads and maximizes TCP/IP data throughput.
  • Microsoft Word:在后台语法和拼写检查器工作时编辑文档以添加所有绿色和红色波浪线下划线。
  • Microsoft Excel:单元格编辑后自动后台重新计算
  • Web 浏览器:在单个页面加载期间分派多个线程以并行加载多个 HTML 引用中的每一个。加速页面加载并最大化 TCP/IP 数据吞吐量。

回答by stevex

These days, the answer should be Any application that can be.

如今,答案应该是任何可以是.

The speed of execution for a single thread pretty much peaked years ago - processors have been getting faster by adding cores, not by increasing clock speeds. There have been some architectural improvements that make better use of the available clock cycles, but really, the future is taking advantage of threading.

单线程的执行速度在几年前几乎达到顶峰 - 处理器通过增加内核而不是通过增加时钟速度变得更快。已经有一些架构改进可以更好地利用可用的时钟周期,但实际上,未来正在利用线程。

There is a ton of research going on into finding ways of parallelizing activities that we traditionally wouldn't think of parallelizing. Even something as simple as finding a substring within a string can be parallelized.

有大量的研究正在进行,以寻找我们传统上不会考虑并行化的活动的并行化方法。即使像在字符串中查找子字符串这样简单的事情也可以并行化。

回答by Cervo

Basically there are two reasons to multi-thread:

多线程基本上有两个原因:

  1. To be able to do processing tasks in parallel. This only applies if you have multiple cores/processors, otherwise on a single core/processor computer you will slow the task down compared to the version without threads.

  2. I/O whether that be networked I/O or file I/O. Normally if you call a blocking I/O call, the process has to wait for the call to complete. Since the processor/memory are several orders of magnitude quicker than a disk drive (and a network is even slower) it means the processor will be waiting a long time. The computer will be working on other things but your application will not be making any progress. However if you have multiple threads, the computer will schedule your application and the other threads can execute. One common use is a GUI application. Then while the application is doing I/O the GUI thread can keep refreshing the screen without looking like the app is frozen or not responding. Even on a single processor putting I/O in a different thread will tend to speed up the application.

  1. 能够并行处理任务。这仅适用于您有多个内核/处理器的情况,否则在单核/处理器计算机上,与没有线程的版本相比,您将减慢任务速度。

  2. I/O 无论是网络 I/O 还是文件 I/O。通常,如果您调用阻塞 I/O 调用,进程必须等待调用完成。由于处理器/内存比磁盘驱动器快几个数量级(网络甚至更慢),这意味着处理器将等待很长时间。计算机将处理其他事情,但您的应用程序不会取得任何进展。但是,如果您有多个线程,计算机将调度您的应用程序,而其他线程可以执行。一种常见用途是 GUI 应用程序。然后,当应用程序进行 I/O 时,GUI 线程可以不断刷新屏幕,而不会出现应用程序冻结或无响应的情况。即使在单个处理器上将 I/O 放在不同的线程中,也会加快应用程序的速度。

The single threaded alternative to 2 is to use asynchronous calls where they return immediately and you keep controlling your program. Then you have to see when the I/O completes and manage using it. It is often simpler just to use a thread to do the I/O using the synchronous calls as they tend to be easier.

2 的单线程替代方案是使用异步调用,它们立即返回并且您继续控制您的程序。然后您必须查看 I/O 何时完成并管理使用它。使用线程通过同步调用执行 I/O 通常更简单,因为它们往往更容易。

The reason to use threads instead of separate processes is because threads should be able to share data easier than multiple processes. And sometimes switching between threads is less expensive than switching between processes.

使用线程而不是单独的进程的原因是线程应该能够比多个进程更容易地共享数据。有时在线程之间切换比在进程之间切换更便宜。

As another note, for #1 Python threads won't work because in Python only one python instruction can be executed at a time (known as the GIL or Global Interpreter Lock). I use that as an example but you need to check around your language. In python if you want to do parallel calculations, you need to do separate processes.

另请注意,对于 #1 Python 线程将不起作用,因为在 Python 中一次只能执行一条 Python 指令(称为 GIL 或全局解释器锁)。我以此为例,但您需要检查您的语言。在python中如果要进行并行计算,则需要进行单独的处理。

回答by MiniQuark

Many GUI frameworks are multi-threaded. This allows you to have a more responsive interface. For example, you can click on a "Cancel" button at any time while a long calculation is running.

许多 GUI 框架都是多线程的。这使您可以拥有响应更快的界面。例如,您可以在长时间计算运行时随时单击“取消”按钮。

Note that there are other solutions for this (for example the program can pause the calculation every half-a-second to check whether you clicked on the Cancel button or not), but they do not offer the same level of responsiveness (the GUI might seem to freeze for a few seconds while a file is being read or a calculation being done).

请注意,还有其他解决方案(例如,程序可以每半秒暂停一次计算以检查您是否单击了取消按钮),但它们不提供相同级别的响应(GUI 可能在读取文件或进行计算时似乎冻结了几秒钟)。

回答by mghie

All the answers so far are focusing on the fact that multi-threading or multi-processing are necessary to make the best use of modern hardware.

到目前为止,所有答案都集中在多线程或多处理是充分利用现代硬件所必需的这一事实上。

There is however also the fact that multithreading can make life much easier for the programmer. At work I program software to control manufacturing and testing equipment, where a single machine often consists of several positions that work in parallel. Using multiple threads for that kind of software is a natural fit, as the parallel threads model the physical reality quite well. The threads do mostly not need to exchange any data, so the need to synchronize threads is rare, and many of the reasons for multithreading being difficult do therefore not apply.

然而,还有一个事实,即多线程可以使程序员的生活变得更轻松。在工作中,我编写软件来控制制造和测试设备,其中一台机器通常由多个并行工作的位置组成。对这种软件使用多线程是很自然的选择,因为并行线程很好地模拟了物理现实。线程大多不需要交换任何数据,因此很少需要同步线程,因此多线程很难的许多原因因此不适用。

Edit:

编辑:

This is not really about a performance improvement, as the (maybe 5, maybe 10) threads are all mostly sleeping. It is however a huge improvement for the program structure when the various parallel processes can be coded as sequences of actions that do not know of each other. I have very bad memories from the times of 16 bit Windows, when I would create a state machine for each machine position, make sure that nothing would take longer than a few milliseconds, and constantly pass the control to the next state machine. When there were hardware events that needed to be serviced on time, and also computations that took a while (like FFT), then things would get ugly real fast.

这并不是真正的性能改进,因为(也许 5 个,也许 10 个)线程大部分都在休眠。然而,当各种并行进程可以被编码为彼此不知道的动作序列时,这对程序结构来说是一个巨大的改进。我对 16 位 Windows 时代的记忆非常糟糕,当我为每个机器位置创建一个状态机时,确保不会超过几毫秒,并不断将控制权传递给下一个状态机。当需要按时处理硬件事件以及需要一段时间的计算(如 FFT)时,事情会变得非常糟糕。

回答by petr k.

Not directly answering your question, I believe in the very near future, almost every application will need to be multithreaded. The CPU performance is not growing that fast these days, which is compensated for by the increasing number of cores. Thus, if we will want our applications to stay on the top performance-wise, we'll need to find ways to utilize all your computer's CPUs and keep them busy, which is quite a hard job.

不直接回答你的问题,我相信在不久的将来,几乎每个应用程序都需要多线程。这些天 CPU 性能的增长速度没有那么快,这可以通过内核数量的增加来弥补。因此,如果我们希望我们的应用程序在性能方面保持最佳状态,我们需要找到方法来利用所有计算机的 CPU 并让它们保持忙碌,这是一项非常艰巨的工作。

This can be done via telling your programs what to do instead of telling them exactly how. Now, this is a topic I personally find very interesting recently. Some functional languages, like F#, are able to parallelize many tasks quite easily. Well, not THAT easily, but still without the necessary infrastructure needed in more procedural-style environments.

这可以通过告诉您的程序要做什么而不是确切地告诉它们如何来完成。现在,这是我个人最近觉得很有趣的一个话题。一些函数式语言,如 F#,能够很容易地并行化许多任务。嗯,不是那么容易,但仍然没有更多程序式环境所需的必要基础设施。

Please take this as additional information to think about, not an attempt to answer your question.

请将此作为要考虑的附加信息,而不是试图回答您的问题。

回答by thaBadDawg

The kind of applications that needto be threaded are the ones where you want to do more than one thing at once. Other than that no application needs to be multi-threaded.

需要线程化的应用程序是那些您想要一次做不止一件事的应用程序。除此之外,没有应用程序需要是多线程的。

回答by Simon P

Applications with a large workload which can be easily made parallel. The difficulty of taking your application and doing that should not be underestimated. It is easy when your data you're manipulating is not dependent upon other data but v. hard to schedule the cross thread work when there is a dependency.

具有大量工作负载的应用程序可以轻松并行。不应低估接受您的申请并这样做的难度。当您操作的数据不依赖于其他数据时很容易,但是当存在依赖关系时很难安排跨线程工作。

Some examples I've done which are good multithreaded candidates..

我做过的一些例子是很好的多线程候选者..

  • running scenarios (eg stock derivative pricing, statistics)
  • bulk updating data files (eg adding a value / entry to 10,000 records)
  • other mathematical processes
  • 运行场景(例如股票衍生品定价、统计)
  • 批量更新数据文件(例如向 10,000 条记录添加值/条目)
  • 其他数学过程