为什么GUI库通常不是线程安全的? [英] Why are GUI libraries generally not thread-safe?

查看:116
本文介绍了为什么GUI库通常不是线程安全的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说GUI通常不是线程安全的,这似乎适用于多个GUI库(例如MFC和WinForms)。我已经看到了简单地通过从单独的线程更新GUI对象上的一些文本而导致的崩溃或奇怪的行为。为什么即使是最简单的GUI操作通常也不是线程安全的? GUI中是否存在固有的东西使它们不是线程安全的?或者更确切地说,为什么这么少的GUI库声称是线程安全的?

I''ve heard that GUIs are generally not thread-safe, and this seems to be true across multiple GUI libraries (such as MFC and WinForms, for instance). I have seen crashing or odd behavior caused simply by updating some text on a GUI object from a separate thread. Why are even the simplest GUI operations generally not thread-safe? Is there something inherent about GUIs that make them not thread-safe? Or rather, why do so few GUI libraries claim to be thread-safe?

推荐答案

答案很简单:成本和遗产。线程安全确实非常昂贵。在windows甚至win32的开头,没有必要是线程安全的。我今天也不能说大多数应用程序都使用此功能。在大多数情况下,您可以通过对代码和逻辑进行一些组织来减少这些工具的使用,并遵循最佳实践。

所以,我认为这很可能是一个很好的方法让编码人员决定何时使用这些功能,而不是花费大量资源。



MFC的问题是,它很难发展。如果资源成本高昂的东西会成为违约,它就会利用这种力量。因此,您将错过一些非常奇特的功能,这些功能已经存在于其他框架和语言中...



至少在.net中,具有匿名代表等功能lambda表达式,我认为在需要时它真的不难做到(参见这个好的文章 [ ^ ])。它可能更简单 - 也许它将来会出现,但这是语法或语言工具的问题,而不是默认情况下使一切都是线程安全的。
The answer is really simple: cost and legacy. Thread safety is really resource costly. At the beginnings of windows and even win32 there was no real need to be thread-safe. Neither could I say today that most application do use this feature. In most cases you can reduce the usage of such tools to a minimum just by doing some organisation of your code and logic - and following best practices.
So, I think it is most likely a good approach to let coders decide when to use these features, than spending wealth of resources on nothing.

The problem with MFC is, that it can hardly evolve. It would use the strength, if such resource-costly things would become default. Thus you will miss some really fancy features, that are already present in other frameworks and languages...

In .net, at least, with features like anonymous delegates and lambda expressions, I think it is really not hard to do it, when needed (see this good article[^]). It could be even more simple - perhaps it will be in the future, but that''s a matter of syntax or language tools, not of making everything thread-safe by default.


性能是原因,我想。


这篇关于为什么GUI库通常不是线程安全的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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