我应该选择哪个 C++ 信号/插槽库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/359928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Which C++ signals/slots library should I choose?
提问by kshahar
I want to use a signals/slots library in a project that doesn't use QT. I have pretty basic requirements:
我想在不使用 QT 的项目中使用信号/插槽库。我有非常基本的要求:
- Connect two functions with any number of parameters.
- Signals can be connected to multiple slots.
- Manual disconnection of signal/slot connection.
- Decent performance - the application is frame-based (i.e. not event-based) and I want to use the connections in each frame.
- 用任意数量的参数连接两个函数。
- 信号可以连接到多个插槽。
- 手动断开信号/插槽连接。
- 体面的性能 - 应用程序是基于框架的(即不是基于事件的),我想在每个框架中使用连接。
I've read a comparison between libsigc++ and Boost.Signals. I've also read that Boost.Signals suffers from poor performance. However, I know there are other libraries and I'm still not sure which library should I choose.
我已经阅读了libsigc++ 和 Boost.Signals 之间的比较。我还读过 Boost.Signals 性能不佳。但是,我知道还有其他库,但我仍然不确定应该选择哪个库。
Are there any recommendations for a signals/slots library?
对信号/插槽库有什么建议吗?
采纳答案by Klaim
First, try with boost::signal anyway. Don't assume it will not be fast enough until you try in your specific case that is your application
首先,无论如何尝试使用 boost::signal。不要假设它不会足够快,直到您尝试在您的应用程序中的特定情况下
If it's not efficient enough, maybe something like FastDelegatewill suit your needs? (i did'nt try it but heard it was a nice solution in some cases where boost::signal don't seem to suit).
如果它不够高效,也许像FastDelegate这样的东西会满足你的需求?(我没有尝试,但听说在 boost::signal 似乎不适合的某些情况下这是一个不错的解决方案)。
Anyway, if in your application use the signal each frame, it may be worth to replace the signal system by something more simple, like a container that hold objects/functors that will be called each frame. Signal is more made to allow immediate "events" management than to make a loop cycle dynamic (allowing changing the functions called each frame). (I have my own solution(UPDATE: it's very old and archaic now) that i heavily use in a game and for instance i've no problem with the performance, so maybe something similar could help).
无论如何,如果在您的应用程序中每帧都使用信号,那么用更简单的东西替换信号系统可能是值得的,例如一个包含将在每一帧调用的对象/函子的容器。信号更多的是允许立即“事件”管理,而不是使循环循环动态(允许更改每个帧调用的函数)。(我有我自己的解决方案(更新:它现在非常古老和过时),我在游戏中大量使用它,例如我对性能没有问题,所以也许类似的东西会有所帮助)。
回答by Dustin Getz
Very, very fast event libraryon Gamedev.net forms
Gamedev.net 表单上非常非常快的事件库
When profiling some code I'd been working on recently, I was surprised and dismayed to see boost::signals functions floating to the top. For those of you who are unaware, boost::signals is a wonderfully useful signal/slot library which can be used alongside boost::bind for delegate-based event handling such as one sees in C#. It is robust, featureful, and flexible. It is also, I have learned, incredibly, terrifyingly slow. For a lot of people who use boost::signals this is fine because they call events very seldom. I was calling several events per frame per object, with predictable results.
So I wrote my own. Slightly less flexible and featureful. It's optimized for how everyone tends to actually use events. And event invocation is fifteen to eighty times faster than boost::signals.
在分析我最近处理的一些代码时,看到 boost::signals 函数浮动到顶部,我感到惊讶和沮丧。对于那些不知道的人,boost::signals 是一个非常有用的信号/槽库,它可以与 boost::bind 一起用于基于委托的事件处理,例如在 C# 中看到的。它健壮、功能强大且灵活。它也是,我了解到,令人难以置信的,可怕的缓慢。对于很多使用 boost::signals 的人来说,这很好,因为他们很少调用事件。我在每个对象的每帧中调用多个事件,结果可预测。
所以我写了我自己的。灵活性和功能性稍差。它针对每个人实际使用事件的方式进行了优化。事件调用比 boost::signals 快十五到八十倍。
see link
见链接
回答by Catskul
The two you listed are the only two worth while that I'm aware of. Everything that I have seen has shown libsigc++ coming out on top performance wise. As you saw in the comparison, there are some instances where boost's syntax is a little prettier, but just a bit.
你列出的两个是我所知道的仅有的两个。我所看到的一切都表明 libsigc++ 在性能方面表现出色。正如您在比较中看到的,在某些情况下,boost 的语法更漂亮一些,但也只是一点点。
I've personally used libsigc++ and am happy with it. Libsigc++ seems to be used by vastly more projects. A quick look in my package manager lists more than 100 projects dependant on libsigc++2. That alone is enough in my opinion to tilt the balance especially considering the performance advantage and the lack of other significant differences.
我个人使用过 libsigc++ 并且对它很满意。Libsigc++ 似乎被更多的项目使用。快速浏览一下我的包管理器,列出了 100 多个依赖于 libsigc++2 的项目。在我看来,仅这一点就足以倾斜平衡,特别是考虑到性能优势和缺乏其他显着差异。
I say libsigc++2.
我说的是 libsigc++2。
回答by fionbio
Recently inherited a project where connect
was producing too much overhead for our project goals. Profiling revealed the use of a mutex in the signal, which was unneeded given our signal usage. Replaced with a dummy mutex per the documentationwith success. The mutex is "drastically slower", so be sure you need it. This may be useful for others skimming this post.
最近继承了一个项目,该项目connect
为我们的项目目标产生了过多的开销。分析显示在信号中使用了互斥锁,鉴于我们的信号使用情况,这是不需要的。根据文档成功替换为虚拟互斥锁。互斥锁“非常慢”,因此请确保您需要它。这对于浏览这篇文章的其他人可能很有用。
Original
typedef boost::signals2::signal_type<void()>::type signal_type;
原来的
typedef boost::signals2::signal_type<void()>::type signal_type;
New
typedef boost::signals2::signal_type<void(), boost::signals2::keywords::mutex_type<boost::signals2::dummy_mutex> >::type signal_type;
新的
typedef boost::signals2::signal_type<void(), boost::signals2::keywords::mutex_type<boost::signals2::dummy_mutex> >::type signal_type;
回答by Rodrigo Lopez
I would vote for Sigslots, I have tried a couple of the other alternatives (boost, libsig++, FastDelegates) and none seemed to do justwhat I wanted: binding functions together in an anonymous way with automatic on-object-destruction disconnect.
我将投票支持Sigslots,我已经尝试了几个其他的替代品(升压,libsig ++,FastDelegates)和似乎没有这样做只是我想要的东西:在自动上对象的破坏断开匿名方式绑定功能结合在一起。
Sigslots was great for us because it is perfectly readable C++, it is fast, simple, and does the job without getting in the way. One minor thing, if you want to use it from several libraries you might need to add:
Sigslots 对我们来说非常棒,因为它是完全可读的 C++,它快速、简单,并且可以在不碍事的情况下完成工作。一件小事,如果您想从多个库中使用它,您可能需要添加:
COREEXTERN template class COREIMPEXP has_slots<SIGSLOT_DEFAULT_MT_POLICY>;
to avoid already-defined-object-related linking issues.
避免与已定义对象相关的链接问题。
回答by asaenko
I have used boost signals2 library, and it is very slow. On construction of object with boost signals, 99% processor time consumed by boost signals stack. On signals emit with single simle slot also it had very large overhead. I try libsigc++ and it's significantly faster. Libsigc++ seems to be very fast and flexible
Creation of 40000 objects with 9 boost signals and 9 libsigc++ signals:
我使用了 boostsignals2 库,它很慢。在构建具有升压信号的对象时,升压信号堆栈消耗了 99% 的处理器时间。在使用单个简单插槽发出的信号上,它也有很大的开销。我尝试了 libsigc++,它的速度要快得多。Libsigc++ 似乎非常快速和灵活使用 9 个 boost 信号和 9 个 libsigc++ 信号创建 40000 个对象:
回答by Caleb Huitt - cjhuitt
I've used libsigc++ before, and it was pretty straightforward. I don't think it would have much in the way of performance penalties, and indeed I learned to like using slots instead of function pointers in a few places.
我以前使用过 libsigc++,它非常简单。我认为它不会对性能造成太大影响,而且确实我学会了喜欢在一些地方使用插槽而不是函数指针。
One thing to be aware of was that as of the last time I used it (2+ years ago), it was limited to a max of six parameters being passed through the connections.
需要注意的一件事是,截至我上次使用它时(2 年多前),它最多只能通过连接传递六个参数。
I don't have any experience with the boost library, so I can't help you there.
我对 boost 库没有任何经验,所以我无法帮助你。
回答by Caleb Huitt - cjhuitt
I have not used libsig++ but I've read up on it. My previous experience with signals and slots are from Qt and a little from Boost. If you don't have either of them available then you can try out my own signal and slots library (ksignals) that exist both for embedded code (no dynamic memory allocation) and "normal" c++ code (dynamic memory allocation when connecting).
我没有使用过 libsig++,但我已经阅读了它。我以前在信号和插槽方面的经验来自 Qt,还有一点来自 Boost。如果您没有它们中的任何一个可用,那么您可以尝试我自己的信号和插槽库 (ksignals),它同时存在于嵌入式代码(无动态内存分配)和“普通”C++ 代码(连接时动态内存分配)。
You can find it at : www.kjellkod.cc/signalandslots
您可以在以下网址找到它:www.kjellkod.cc/signalandslots
At the the page you can also find a comparison: KSignals Vs Boost signals.
在页面上,您还可以找到比较:KSignals Vs Boost 信号。
Speed vise ksignals is very fast and extremely lightweight code wise. It should be very easy to use, understand and if needed to modify it.
Speed vise ksignals 非常快,而且代码非常轻巧。它应该非常易于使用、理解并且在需要时可以对其进行修改。
Good luck Regards Kjell H
祝你好运 Kjell H
回答by Ivan
One more sig-slot implementation to consider:
另一个需要考虑的 sig-slot 实现:
http://code.google.com/p/ting/wiki/SignalSlotUsage
http://code.google.com/p/ting/wiki/SignalSlotUsage
It does not pretend to be the best one, but, still, another one which has its right to exist.
它并不假装是最好的,但仍然是另一个有权存在的。
回答by yigityuce
An another option can be YSignalSlot. I used it. I think it is pretty good.
另一个选项可以是YSignalSlot。我用过。我觉得还不错。