线程:网络线程、gui线程、后端线程 邮局中介类设置好了吗?
我倾向于使用以下作为我的标准线程模型,但也许它不是一个很好的模型。人们还有什么其他建议或者他们认为这个设置很好?这不适用于高性能互联网服务器,尽管性能有时非常关键,在这些情况下我使用异步网络方法并重用缓冲区,但它是相同的模型。
有一个 gui 线程来运行 gui。
有一个后端线程可以处理计算密集型的任何内容(基本上是 gui 可以传递但运行速度不太快的任何内容),并且还负责解析传入消息或 gui 操作并对其进行操作。
有一个或多个网络线程负责在必要时将传出的发送分成几部分,从各种套接字接收数据包并将它们重新组装成消息。
有一个中间静态类充当网络和后端线程之间的中介。它充当邮局。需要发出的消息由后端线程发布到它,网络线程检查此类的“发件箱”以查找要发送的消息并将任何传入消息发布到此类具有的静态“收件箱”中(无论它们到达的套接字如何)来自,尽管该信息是与传入消息一起发布的),后端线程会检查该消息以查找来自它应该执行操作的其他计算机的消息。
gui/后端线程接口往往更加临时,并且应该有自己的邮局,例如类或一些替代中介?
对这个线程设置有什么意见/建议吗?
I tend to use the following as my standard threading model, but maybe it isn't such a great model. What other suggestions do people have or do they think this is set up well? This is not for a high performance internet server, though performance is sometimes pretty critical and in those cases I use asynchronous networking methods and reuse buffers, but it is the same model.
There is a gui thread to run the gui.
There is a backend thread that handles anything that is computationally intensive (basically anything the gui can hand off that isn't pretty quick to run) and also is in charge of parsing and acting on incoming messages or gui actions.
There is one or more networking threads that take care of breaking an outgoing send into peices if necessary, recieving packets from various sockets and reassembling them into messages.
There is an intermediary static class which serves as an intermediary between the networking and backend threads. It acts as a post office. Messages that need to go out are posted to it by backend threads and networking threads check the "outbox" of this class to find messages to send and post any incoming messages in a static "inbox" this class has (regardless of the socket they arrive from, though that information is posted with the incoming message) which the backend thread checks to find messages from other machines it should act on.
The gui / backend threading interface tends to be more ad hoc and should probably have its own post office like class or some alternative intermediary?
Any comments/suggestions on this threading setup?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我主要担心的是,您并不真的想将自己锁定在只能有一个后端线程的想法中。我的正常模型是首先使用 MVC,确保我使用的所有数据结构对于线程环境来说本质上不是不安全的,避免单例,然后疯狂地进行分析,一边尝试一边尽量减少数量,一边进行分析。我正在利用的条件变量。对于长时间的异步任务,我更喜欢生成一个新进程,特别是如果它可能想让操作系统赋予它不同的优先级。
My primary concern is that you don't really want to lock yourself into the idea that there can only be one back-end thread. My normal model is to use the MVC at first, make sure all the data structures I use aren't inherently unsafe for a threaded environment, avoid singletons, and then profile like crazy, splitting things out as I go while trying to minimize the number of condition variables I'm leveraging. For long asynchronous tasks, I prefer to spawn a new process, particularly if it's something that might want to let the OS give it a differing priority.
这种架构听起来像经典的 模型-视图-控制器 通常被认为是好的架构。
This architecture sounds like the classic Model-View-Controller architecture which is usually considered as good.