线程:网络线程、gui线程、后端线程 邮局中介类设置好了吗?

发布于 2024-10-05 19:15:59 字数 537 浏览 2 评论 0原文

我倾向于使用以下作为我的标准线程模型,但也许它不是一个很好的模型。人们还有什么其他建议或者他们认为这个设置很好?这不适用于高性能互联网服务器,尽管性能有时非常关键,在这些情况下我使用异步网络方法并重用缓冲区,但它是相同的模型。

有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无敌元气妹 2024-10-12 19:15:59

我主要担心的是,您并不真的想将自己锁定在只能有一个后端线程的想法中。我的正常模型是首先使用 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.

喜你已久 2024-10-12 19:15:59

This architecture sounds like the classic Model-View-Controller architecture which is usually considered as good.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文