Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which C++ signals/slots library should I choose? [closed]

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.
  2. Signals can be connected to multiple slots.
  3. Manual disconnection of signal/slot connection.
  4. 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.

Are there any recommendations for a signals/slots library?

like image 647
kshahar Avatar asked Dec 11 '08 16:12

kshahar


People also ask

What is signals and slots in Qt?

In Qt, we have an alternative to the callback technique: We use signals and slots. A signal is emitted when a particular event occurs. Qt's widgets have many predefined signals, but we can always subclass widgets to add our own signals to them. A slot is a function that is called in response to a particular signal.

Are signals and slots thread safe?

It is generally unsafe to provide slots in your QThread subclass, unless you protect the member variables with a mutex. On the other hand, you can safely emit signals from your QThread::run() implementation, because signal emission is thread-safe.

In what order will the slots be executed if they are connected to one signal?

According to Qt documentation: If several slots are connected to one signal, the slots will be executed one after the other, in the order they have been connected, when the signal is emitted.

What is emit in c++?

emit is just syntactic sugar. If you look at the pre-processed output of function that emits a signal, you'll see emit is just gone. The "magic" happens in the generated code for the signal emitting function, which you can look at by inspecting the C++ code generated by moc.


2 Answers

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

like image 118
Klaim Avatar answered Sep 30 '22 07:09

Klaim


Very, very fast event library on Gamedev.net forms

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.

see link

like image 28
Dustin Getz Avatar answered Sep 30 '22 09:09

Dustin Getz