gpt4 book ai didi

c++ - 是否有一些用于模拟 Glib::Dispatcher 的 Boost 功能?

转载 作者:太空狗 更新时间:2023-10-29 23:19:06 25 4
gpt4 key购买 nike

我目前正在重构一个中型软件项目。它包含一个由多个线程使用的类似内核的中央类。目前,此类使用 Glib::Dispatcher 来处理由多个线程发出的信号。由于重构过程的一个目标是完全摆脱 glibmm(因为 Qt 将用作新框架),我试图找出一种方法使用 Boost“模拟”调度程序功能。我已经研究过 Boost.SignalsBoost.Signals2,但这些库似乎都没有提供调度程序的替代方案。

为了阐明调度员应该做什么,这里是来自 the official documentation 的简短描述。 :

Glib::Dispatcher works similar to sigc::signal. But unlike normal signals, the notification happens asynchronously through a pipe. This is a simple and efficient way of communicating between threads, and especially useful in a thread model with a single GUI thread.

No mutex locking is involved, apart from the operating system's internal I/O locking. That implies some usage rules:

  • Only one thread may connect to the signal and receive notification, but multiple senders are allowed even without locking.
  • The GLib main loop must run in the receiving thread (this will be the GUI thread usually).
  • The Dispatcher object must be instantiated by the receiver thread.
  • The Dispatcher object should be instantiated before creating any of the sender threads, if you want to avoid extra locking.
  • The Dispatcher object must be deleted by the receiver thread.
  • All Dispatcher objects instantiated by the same receiver thread must use the same main context.

你能给我一些正确方向的指示吗?这是我可以使用 Boost.SignalsBoost.Signals2 实现的那种功能吗?

编辑:正如评论者正确指出的那样,使用 Qt 可能是一种选择。但是,我正在重构的类非常底层,我不想添加这个额外的依赖项。

最佳答案

我认为没有简单的方法可以做到这一点,以 boost 的形式删除 Glib 不会解决这个问题,它更像是一个架构问题,而不是其他任何问题。用 Boost 替换不会解决设计问题。您应该为自己的信号接口(interface)建模,并尝试适应每个库,首先包括 Glib,因为它已经在工作,为您的问题添加另一个间接级别将让您解决该问题。

如果您查看 boost::function,Boost 可以为您提供帮助.我不认为用 boost 替换 glib 是一个真正的进步,boost 不是一个图形库,在某些时候需要它来为你的图形引擎添加一个带有实现层的接口(interface)。

关于c++ - 是否有一些用于模拟 Glib::Dispatcher 的 Boost 功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10744152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com