If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Callbacks are a class of functions. There are many kinds of callbacks in all different types of programming. In general, a callback function is used as a delayed reaction to some kind of input or after some result.

Callback techniques are often used in multi-process or multi-threaded applications in order to provide communication mechanisms among threads and processes.

The example below might be a little bit tough to understand. But please do take a look. The windows stuff is all standard. It will give you the right ideas. It uses Microsoft compilers in the Win32 API environment. Try to see how the callback works.

"callback_calculation_done()" is not a callback function here. " thread_do_calculation()" qualifies as one though not in the traditional sense. A callback function simply refers to a pointer to a function that you hand off to someone else so the function can later be invoked (called back) when something interesting happens. For instance, take a look at the Windows function "EnumWindows()". The first argument is a callback function that "EnumWindows()" will invoke (call back) once for each top-level window on the system. This is what is normally meant by a "callback". BTW, your use of "b_done" to block the main thread is inefficient. You should rely on methods like passing the thread handle to "WaitForSingleObject()" for instance. More importantly, you're accessing a global (static) variable across two threads without providing synchronization control (which is rather harmless in this trivial example but the technique in general is dangerous).

This can be extended to allow Foo to have a vector of observers (they'd have to be stored as pointers, not references, though). Also Bar can be an observer of more than one class by inheriting from multiple observer interfaces.

Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.-- Sutter and Alexandrescu, C++ Coding Standards

Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman

The cheapest, fastest and most reliable components of a computer system are those that aren't there. -- Gordon Bell

I don' t know where you found this, but it's a terrible example of anything.

The use of _ to distinguish variables of buttons (preceeding AND trailing) is abysmal and will lead to bugs almost certainly.

Secondly, it's not an example of callbacks. It's a strange example of how to pass an object into another object, and then that other object using it. Nothing unusual at all.

By the way, I program radios that drive CD mechanisms for a living. This is not the way you want to go about this sort of thing. It's almost reminiscent of the Microsoft tuner APIs built into Apollo, sorry, AutoPC, sorry, Windows CE for Automotive.

I suggest looking into good ol' qsort to see what a callback function is all about.