哪些类型的应用程序需要多线程? [英] What kinds of applications need to be multi-threaded?

查看:1272
本文介绍了哪些类型的应用程序需要多线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是需要多线程或不需要多线程的应用程序的一些具体示例?

What are some concrete examples of 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.

推荐答案

虽然没有一成不变的答案,但是在大多数情况下,对于工作流/计算是顺序的系统,您将看不到任何优势.但是,如果问题可以分解为可以并行运行的任务(或者问题本身是大规模并行的(例如某些数学或分析问题)),则可以看到很大的改进.

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.

一些例子

  • 图像处理通常可以并行完成(例如,将图像分成4张并在1/4的时间内完成工作),但这取决于运行的算法是否能够感.
  • 动画渲染(来自3DMax等)在很大程度上是并行的,因为每个帧都可以独立于其他帧渲染-意味着可以将10或100台计算机链接在一起以提供帮助.
  • GUI 编程通常在执行缓慢的操作时(例如,处理大量文件-这可以使界面在工作人员辛苦工作的同时保持响应速度(在C#中,BackgroundWorker就是其中的一个示例)
  • Image processing can 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.
  • GUI programming 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)

GUI是一个有趣的领域,因为如果工作程序算法通过在Windows API术语(.NET等之前)中给它时间留出时间来使主GUI保持活动"状态,则可以在不使用多线程的情况下保持接口的响应性".这可以通过原始循环而不需要线程来实现:

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:

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)
}

这篇关于哪些类型的应用程序需要多线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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