How callback function works.

J

Jianli Shen

following code:

some_file.c
void serverstream_metrics_register_obtain_bandwidth_cb(IntCallbackFunc
*cb) {
serverstream_metrics_obtain_bandwidth_cb = cb;
}

some_file.h
typedef int IntCallbackFunc(void); //what this means? IntCallbackFunc is a
type of int?
void serverstream_metrics_register_obtain_bandwidth_cb(IntCallbackFunc *cb);

handler_file.h
int obtain_latency_cb();

handler_file.c
int obtain_latency_cb() {
return (int)latency; // this value will finaly give to which parameter
in this program?
}


main_file.c
#include some_file.h
#include handler_file.h

main {
....
serverstream_metrics_register_obtain_bandwidth_cb(obtain_latency_cb);
....
}


Can anyone please explain why we use callback function in C/C++ , and how to
use it!

Thanks
 
J

Jianli Shen

miss something:
following code:

some_file.c

IntCallbackFunc *serverstream_metrics_obtain_bandwidth_cb = NULL;
void serverstream_metrics_register_obtain_bandwidth_cb(IntCallbackFunc
*cb) {
serverstream_metrics_obtain_bandwidth_cb = cb;
}

and ok there is
perf_metrics->bandwidth = (*serverstream_metrics_obtain_bandwidth_cb)();
so the bandwidth return by obtain_bandwidth_cb() is finally give to
perf_metric->bandwidth
 
S

Sais

some_file.h
typedef int IntCallbackFunc(void); //what this means? IntCallbackFunc is
a type of int?

Probably the best way to read this is that "IntCallbackFunc" is a
function that takes (void) parameters and returns an int.

So, when you define:

void serverstream_metrics_register_obtain_bandwidth_cb(IntCallbackFunc
*cb);

you are saying that cb is a pointer to a funtion that takes no params,
and returns int. i.e. it is a _function pointer_. That means that, as
long as you pass in something that is a function of the right type,
serverstream_metrics_register_obtain_bandwidth_cb will use that
function wherever it calls cb.

This is useful in various cases - here it might be that you have
multiple ways of calculating the latency that is used in calculating
the b/w.

A callback function is one that will handle the results of the
processing when they are done. If you call into a function that starts
a thread to do some calculation, then it will return once the thread
has started. That doesn't mean that the calculation has been performed,
just that you are now free to keep on doing what you need while the
calculation happens. By passing in a callback function, you give the
thread a place to call back to and handle the result of the calculation
once it's done.

There are most probably more lucid descriptions around - google for
function pointers and for callback functions. The latter is a special
case of the former.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,661
Latest member
FloridaHan

Latest Threads

Top