Call Back functions

This is a discussion on Call Back functions within the C Programming forums, part of the General Programming Boards category; Hi people well I was trying to write a little application and a part of it receives a message from ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    9

    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

  2. #2
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    this isnt exactly an easy thing to do. what OS do you intend to accomplish this under?
    hello, internet!

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    9
    I am running on a propriotary RTOS

  4. #4
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    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
    }
    Last edited by Cshot; 08-21-2002 at 02:48 PM.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    9
    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()

  6. #6
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Why can't you call the callback function at the end of Application when it's done doing it's tasks?
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    9
    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

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    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;
    }
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    85
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing back objects from with functions. Datatype?
    By Swerve in forum C++ Programming
    Replies: 3
    Last Post: 03-20-2009, 11:50 AM
  2. How properly inherit from template?
    By 6tr6tr in forum C++ Programming
    Replies: 118
    Last Post: 04-25-2008, 04:30 AM
  3. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 03:48 PM
  4. Attaching functions to a class/struct
    By VirtualAce in forum Tech Board
    Replies: 2
    Last Post: 08-04-2003, 10:56 AM
  5. Calling App Functions from DLL
    By johnnie2 in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2003, 12:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21