Which C++ signals/slots library should I choose?

前端 未结 11 1400
隐瞒了意图╮
隐瞒了意图╮ 2020-12-01 00:10

I want to use a signals/slots library in a project that doesn\'t use QT. I have pretty basic requirements:

  1. Connect two functions with any number of parameters
相关标签:
11条回答
  • 2020-12-01 00:52

    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.

    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.

    I don't have any experience with the boost library, so I can't help you there.

    0 讨论(0)
  • 2020-12-01 00:53

    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

    If it's not efficient enough, maybe something like FastDelegate will 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).

    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).

    0 讨论(0)
  • 2020-12-01 00:53

    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 documentation with success. The mutex is "drastically slower", so be sure you need it. This may be useful for others skimming this post.

    Original 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;

    0 讨论(0)
  • 2020-12-01 00:56

    I would vote for Sigslots, I have tried a couple of the other alternatives (boost, libsig++, FastDelegates) and none seemed to do just what I wanted: binding functions together in an anonymous way with automatic on-object-destruction disconnect.

    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:

    COREEXTERN template class COREIMPEXP has_slots<SIGSLOT_DEFAULT_MT_POLICY>;
    

    to avoid already-defined-object-related linking issues.

    0 讨论(0)
  • 2020-12-01 01:00

    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:

    0 讨论(0)
提交回复
热议问题