-
Call Back functions
Hi people
well I was trying to write a little application and a part of it receives a message from another application to do something. But the calling application cannot wait till the util application is done. Thus the calling application sends a call back function for the util to call once the util function is done.
can any one show a piece of siple code which demonstrate this phenomenon.
thanks
-
this isnt exactly an easy thing to do. what OS do you intend to accomplish this under?
-
I am running on a propriotary RTOS
-
Use semaphores. The code would be OS dependent though. Send the callback with the semaphore taken. Once the app is done, release the semaphore so the util can execute the callback function.
Code:
void MainApp()
{
// CreateSemaphore(...)
// invoke callback function here
// do stuff
// ReleaseSemaphore()
}
void UtilCallback()
{
// WaitForSemaphore()
// do stuff
}
-
I think I may not be clear in my question
let me try again
The Application() calls the Util_fun() to perform a task but this task will take some time to finish and the application does not want to wait for that. So one solution is Application() passes a callback() function to the Util() and util save it. Once Util is done with the task then it will call the CallBack() function.
Application ()
{
CALL Util(*callback())
}
Util(CallBack())
{
SAVE CallBack();
START Task()
Pass Control Back to the Application()
}
Later on in time when Task() is done
Util() uses the saved CallBack() function to send back the status to Application()
-
Why can't you call the callback function at the end of Application when it's done doing it's tasks?
-
i think the intent is to have a mechanism that the application wants to have the callback function when the Util task is done. Thus the callback function will be triggered by the util ().
The reason could be for e.g if we have multiple Applications calling th esame util() but once the util performs the task then all the applications want to called a different callback().
The think I am trying to understand is how to pass the callback function name to the util ().
any sample code should help
thanks
-
This illlustrates how you can do this, and passing a parameter to the callback as well. Here is the basic idea:
Code:
#define ALLS_WELL 1
#define SOME_ERROR 0
void Launch( void callback_fn(int), int callback_param)
{
printf("Launching Utility...\n\n");
//...do work...
if(1 == 1)/*some condition*/
callback_param = ALLS_WELL;
else
callback_param = SOME_ERROR;
callback_fn( callback_param );
}
void Callback(int param)
{
printf("Entering Callback...\n\n");
if(param == ALLS_WELL)
{
printf("Launch() was successful...\n\n");
}
else
{
printf("Launch() failed...\n\n");
}
}
int main()
{
int param; //..fill in later...
Launch(Callback, param);
getch();
return 0;
}
-
The think I am trying to understand is how to pass the callback function name to the util ().
any sample code should help.
-->Use pointer to function for callback() and
pass it into util() function. Aslo you can
implement a event handling to signal the
callback() whenever you need it on or off.
Could you show your code so that we might able
to help.
DV007