有关GUI应用程序中的线程数的最佳实践 [英] Best practice regarding number of threads in GUI applications

查看:105
本文介绍了有关GUI应用程序中的线程数的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去,我与许多专门从事GUI应用程序编写工作的程序员一起工作.

In the past I've worked with a number of programmers who have worked exclusively writing GUI applications.

给我的印象是,它们几乎普遍地最小化了在其应用程序中对多个线程的使用.在某些情况下,它们似乎已竭尽全力以确保使用单个线程.

And I've been given the impression that they have almost universally minimised the use of multiple threads in their applications. In some cases they seem to have gone to extreme lengths to ensure that they use a single thread.

这很常见吗?这是GUI应用程序设计的普遍接受的哲学吗?

Is this common? Is this the generally accepted philosophy for gui application design?

如果是的话,为什么?

有许多答案表明应尽量减少线程使用以降低复杂性.通常,降低复杂性是一件好事.

There are a number of answers saying that thread usage should be minimised to reduce complexity. Reducing complexity in general is a good thing.

但是,如果您查看对外部事件的响应至关重要的任何应用程序(例如,Web服务器,任何数量的嵌入式应用程序),则在使用线程的态度上似乎存在很大差异.

But if you look at any number of applications where response to external events is of paramount importance (eg. web servers, any number of embedded applications) there seems to be a world of difference in the attitude toward thread usage.

推荐答案

我见过同样的东西.理想情况下,您应该在后台线程中执行将花费比数百毫秒更长的时间的任何操作.任何超过100毫秒的分类器和人类都可能不会注意到差异.

I've seen the same thing. Ideally you should perform any operation that is going to take longer then a few hundred ms in a background thread. Anything sorter than 100ms and a human probably wont notice the difference.

我过去使用过的许多GUI程序员都害怕线程,因为它们是硬"的.在某些GUI框架(例如Delphi VCL)中,存在有关从多个线程使用VCL的警告,这往往使某些人感到恐惧(其他人则将其视为挑战;))

A lot of GUI programmers I've worked with in the past are scared of threads because they are "hard". In some GUI frameworks such as the Delphi VCL there are warnings about using the VCL from multiple threads, and this tends to scare some people (others take it as a challenge ;) )

一个有趣的多线程GUI编码示例是BeOS API.应用程序中的每个窗口都有其自己的线程.根据我的经验,这使BeOS应用程序感觉更灵敏,但确实使编程变得有些棘手.幸运的是,由于BeOS默认情况下是设计为多线程的,因此API中有很多东西可以使事情比我所使用的其他OS更加容易.

One interesting example of multi-threaded GUI coding is the BeOS API. Every window in an application gets its own thread. From my experience this made BeOS apps feel more responsive, but it did make programming things a little more tricky. Fortunately since BeOS was designed to be multi-threaded by default there was a lot of stuff in the API to make things easier than on some other OSs I've used.

这篇关于有关GUI应用程序中的线程数的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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