Jump to content
  • Advertisement
Sign in to follow this  
Anton Vatchenko

Best callback technique

This topic is 2788 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!