Best callback technique

Recommended Posts

What is the best practice for C++:
1. Static pointers.
2. Templates (functors).
3. Interfaces (usually virtual inheritance).

Best means:
1. Good code style (static functions are non-stable in casting for wrong instances passed).
2. Good speed (if there is about thousand callbacks per frame).

Share on other sites
Does anyone of 50 people who have already looked here uses callbacks?

Share on other sites
SiCrane    11839
Theres no such thing as a single best method for doing callbacks. If you're worried about performance, profile the different techniques yourself.

Also, in the future don't bump your threads when less than two hours have passed. The general programming FAQ lists the time before a bump to be twenty four hours.

Share on other sites
Eddycharly    437
i would say generated free functions by templates are a good solution (because free functions pointers have a fixed size), that's not trivial to write such code though.

interfaces is probably the simplest method but it is not very flexible imo.

Share on other sites
Telastyn    3777
Depends on your requirements, but I tend to favor boost::function objects.

Share on other sites
Antheus    2409
Quote:
 Original post by Anton Vatchenko2. Good speed (if there is about thousand callbacks per frame).

This type of problems is better solved using queues. Rather than invoking callbacks, have a queue of results. Given an example of collision detection:
struct CollisionPair {  Foo * a;  Foo * b;};vector<CollisionPair> results;find_collisions(data, results);handle_collisions(results);

Quote:
 What is the best practice for C++:

boost::function. Solves all the millions of edge cases involved.

Share on other sites
I wrote a simple tests for functors (templates), interfaces and statics. In most 2-processor and huge_gb-RAMs static functions are 0ms per 100 million callbacks, meanwhile interfaces (with virtual diamond-like inheritance) look like the slowest ones. But on old machines static functions look like the slowest.

Share on other sites
Antheus    2409
Quote:
 Original post by Anton VatchenkoI wrote a simple tests for functors (templates), interfaces and statics. In most 2-processor and huge_gb-RAMs static functions are 0ms per 100 million callbacks
That just means compiler optimized them out.

Dispatching 100 million NOP callbacks will take on the order of 100ms under ideal circumstances.

Real messaging systems can handle in the order of 100k/second.

Share on other sites
alvaro    21246
My favorite method is boost::signal.

Share on other sites
Quote:
 Original post by alvaroMy favorite method is boost::signal.

Seconded. Well, boost::signals2, but there's little difference between the two for most uses.

Share on other sites
Uziel2101    242
I have to agree that the system of using "signals" and "slots" via boost / Qt is the most efficient way of doing callbacks when I need them.

Share on other sites
My favourite method is the interface based callback - its cheap and easy to implement. boost signal / slots are a more powerful alternative if you like to bind individual functions as the callback receivers instead of a whole class.

Share on other sites
Decrius    100
Boost Signal / Slots is a very good solution on all fronts, but it's speed might be lacking for you. For my specific tests, SigC++ was twice as fast (but measure this for your particular use!). I wrote my own limited kind of signal/slots, which is again 3 times faster, roughly.

So, check for yourself. Boost is truely not a fast solution for signals/slots, but it sure is stable, compatible, etc.

Share on other sites
alvaro    21246
Quote:
 Original post by DecriusBoost Signal / Slots is a very good solution on all fronts, but it's speed might be lacking for you.

Quote:
 Original post by Anton Vatchenko2. Good speed (if there is about thousand callbacks per frame).

I can't imagine a thousand callbacks per frame being a performance problem.

Share on other sites
if you imagine about 200 frames per second, and this callback system is not the main target in application.

Share on other sites
alvaro    21246
Quote:
 Original post by Anton Vatchenkoif you imagine about 200 frames per second, and this callback system is not the main target in application.

In a quick test on my computer, it looks like the overhead would take about 3% of the CPU under those circumstances. But such a simple synthetic test doesn't mean much, so you will have to do your own profiling.

We can't help you decide what to use: We can only point out alternatives for you to try. If you can budget boost::signals, they are a very elegant way to do things. An old-fashioned function pointer is much much faster, so I am sure there will be some speed/elegance compromise that you can use.

Share on other sites
Antheus    2409
Quote:
 Original post by Anton Vatchenkoif you imagine about 200 frames per second, and this callback system is not the main target in application.

boost::function is fine then. It's ISO certified up to 220 (160 in Imperial units) frames per second.

Share on other sites
Quote:
Original post by Antheus
Quote:
 Original post by Anton Vatchenkoif you imagine about 200 frames per second, and this callback system is not the main target in application.

boost::function is fine then. It's ISO certified up to 220 (160 in Imperial units) frames per second.

LOL!