所有事件驱动的框架都应该是单线程的? [英] Should all event-driven frameworks be single-threaded?

查看:94
本文介绍了所有事件驱动的框架都应该是单线程的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://weblogs.java.net/blog /kgh/archive/2004/10/multithreaded_t.html 认为,多线程GUI框架是一个失败的梦想。非GUI框架呢?这个经验法则是否适用于所有事件驱动的框架?

http://weblogs.java.net/blog/kgh/archive/2004/10/multithreaded_t.html argues that multithreaded GUI frameworks are a failed dream. What about non-GUI frameworks? Does this rule of thumb extend to all event-driven frameworks?

这是引起我注意的文章的引用:

Here is a quote from the article that caught my attention:



输入事件处理的问题是它往往与大多数GUI活动的方向相反。一般来说,GUI操作从库抽象堆栈的顶部开始,并下。我在我的应用程序中运行一个由一些GUI对象表达的抽象概念,所以我从我的应用程序开始,并调用高级GUI抽象,调用低级GUI抽象,调用到丑恶的内容工具包,然后进入操作系统。相比之下,输入事件在OS层开始,并逐渐派发出抽象层,直到它们到达我的应用程序代码。

The problem of input event processing is that it tends to run in the opposite direction to most GUI activity. In general, GUI operations start at the top of a stack of library abstractions and go "down". I am operating on an abstract idea in my application that is expressed by some GUI objects, so I start off in my application and call into high-level GUI abstractions, that call into lower level GUI abstractions, that call into the ugly guts of the toolkit, and thence into the OS. In contrast, input events start of at the OS layer and are progressively dispatched "up" the abstraction layers, until they arrive in my application code.

现在,因为我们是使用抽象,我们自然会在每个抽象中分别进行锁定。不幸的是,我们有经典的锁定订购噩梦:我们有两种不同的活动,希望以相反的顺序获得锁。所以死锁几乎是不可避免的。

Now, since we are using abstractions, we will naturally be doing locking separately within each abstraction. And unfortunately we have the classic lock ordering nightmare: we have two different kinds of activities going on that want to acquire locks in opposite orders. So deadlock is almost inevitable.



推荐答案

说不,有一个警告。围绕共享状态的事件驱动框架,例如UI,应该是单线程的。事件驱动的框架围绕通知,例如机械监控(例如,让管道中的压力过高),可以是单线程的,但可能更适合多线程。

I would have to say no, with a caveat. Event driven frameworks that revolve around shared state, such as a UI, should be single threaded. Event driven frameworks that revolve around notifications, such mechanical monitoring(e.g. letting you know when the pressure in a pipe is too high), could be single threaded, but might be more appropriate to be multi threaded.

当然有可能构建一个多线程的UI框架,我已经做到了这一点。最后我把它转换为单线程。部分原因在于查理所说的太难了。问题是,使用多线程的UI框架,不仅仅是我要处理线程,而是使用UI的任何人。核心当然是线程安全的,但是任何编写控件的人都必须使线程安全。不要犹豫,当对UI进行多次更改时,您必须通知核心您正在做,所以您没有得到部分更新。由于用户通常是一个非常缓慢的事情,任何性能增益都是可以忽略的,如果需要特定的情况,可以使用异步调用来处理。单线程实际上是这里的适当的模型。

It is certainly possible to build a multi threaded UI framework, and I have done so myself. In the end I converted it to be single threaded. Part of the reason does fall under what Charlie said about "it's too hard". The problem was that with a multi-threaded UI framework, it wasn't just me that had to deal with the threading, but anyone that used the UI. The core was certainly thread safe, but then anyone that wrote a control had to make that thread safe as well. Nevermind that when making multiple changes to the UI, you had to notify the core that you were doing so so you didn't get partial updates. Since a user is generally a pretty slow thing, any performance gain was really negligible, and could be worked around with an asynchronous call if necessary for specific cases. Single threading is actually the appropriate model here.

另一方面,在没有(或不是很多)共享状态的模型中,多线程模型显得有意义。一个事件(反应堆处于起火状态)没有任何理由延迟30秒的时间,因为您的查询(Bob操作员刚刚计时)需要超时,因为某些操作中的某个操作已将punch_card表锁定。

On the other hand, in a model where there isn't(or isn't much) shared state, a multi threaded model makes eminent sense. There's no reason for one event(the reactor is on fire) to be delayed for the 30 seconds it takes for your query(Bob the operator just clocked out) to timeout because some yahoo in operations left the punch_card table locked.

这篇关于所有事件驱动的框架都应该是单线程的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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