Best callback technique

Started by
16 comments, last by doesnotcompute 13 years, 4 months ago
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).
VATCHENKO.COM
Advertisement
Does anyone of 50 people who have already looked here uses callbacks?
VATCHENKO.COM
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.
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.
Depends on your requirements, but I tend to favor boost::function objects.
Quote:Original post by Anton Vatchenko
2. 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.
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.
VATCHENKO.COM
Quote:Original post by Anton Vatchenko
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
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.

My favorite method is boost::signal.
Quote:Original post by alvaro
My favorite method is boost::signal.

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

This topic is closed to new replies.

Advertisement