473,395 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How callback function works.

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
Jul 23 '05 #1
2 2071
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
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_bandwidth_cb();

handler_file.c
int obtain_bandwidth_cb() {
return (int)bandwidth; // 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

Jul 23 '05 #2
> 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.

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mohamed Fysal | last post by:
I have written a Regular DLL with many Export Functions and one CALLBACK fun ction . The callback function declared in the .cpp file of the Regular DLL is as fol lows: typedef BOOL...
0
by: bob | last post by:
Hi all Currently porting an application to managed C++ Have managed to create working MDI form and child windows etc. Also added a callback from an unmanaged 'C' dll (ok that was strangely...
3
by: christophe.verne | last post by:
Hi all, It's the first time I'm using groups so i hope I am posting on the good group. I''m not familiar with callback and function pointer and here is what I'd like to do: typedef struct...
7
by: Kirk McDonald | last post by:
Let's say I have a function that takes a callback function as a parameter, and uses it to describe an iteration: def func(callback): for i in : callback(i) For the sake of argument, assume...
4
by: Kyku | last post by:
Hello all, I'm in a process of writing a small Linux C++ program for discovering repeaded files over a filesystem. One part of this task is traversing a directory structure. This is done by the...
5
by: sajin | last post by:
Hi All.. We are using VB .Net 2005 for implementing an API. API needs to generate events. For this client wants us to use Windows Callback (delegate implementation). The intention of using...
10
by: SQACPP | last post by:
Hi, I try to figure out how to use Callback procedure in a C++ form project The following code *work* perfectly on a console project #include "Windows.h" BOOL CALLBACK...
40
by: Angus | last post by:
Hello I am writing a library which will write data to a user defined callback function. The function the user of my library will supply is: int (*callbackfunction)(const char*); In my...
0
by: Tim Spens | last post by:
--- On Fri, 6/27/08, Tim Spens <t_spens@yahoo.comwrote: I think I know where the problem is but I'm unsure how to fix it. When I call Register_Handler(...) from python via...
5
by: Jef Driesen | last post by:
I have a C DLL that I want to use from a C# project. The C header file contains these declarations: typedef void (*callback_t) (const unsigned char *data, unsigned int size, void *userdata);...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.