Thread: Callback functions concept

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    22

    Thumbs down Callback functions concept

    Hi all,

    Can you please explain how Callback functions work.?

    I see in some applications, if we use callback function inside any function, first that main function completes then control is going to That Callback function.....

    So can we use that direct function call to that Function(callback), at the end of that main function....?

    what is the difference in both two...?


    Please make light on this....


    Thanks
    Bhupesh

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by bhupesh.kec View Post
    I see in some applications, if we use callback function inside any function, first that main function completes then control is going to That Callback function.....
    If you call the function inside other function the first will not exit till the second is finished.
    To perform a call asynchroneously - some timer mechanism can be used. So instead of calling the second function directly the first function only sets a timer to very short timeout (for example 0) and exits

    When the event loop is processed the corresponding timer event is retreived from the queue and the function bind to the event is called...

    It has a very little connection to the callback mechanism itself. Callback function is just a function called by the pointer provided by the application to some module like dll that cannot be linked to the function in the main application directly.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    22

    Red face

    Quote Originally Posted by vart View Post
    If you call the function inside other function the first will not exit till the second is finished.
    To perform a call asynchroneously - some timer mechanism can be used. So instead of calling the second function directly the first function only sets a timer to very short timeout (for example 0) and exits

    When the event loop is processed the corresponding timer event is retreived from the queue and the function bind to the event is called...

    It has a very little connection to the callback mechanism itself. Callback function is just a function called by the pointer provided by the application to some module like dll that cannot be linked to the function in the main application directly.

    Hi Vart
    Thanks a lot for ur kind reply...
    Now i got some idea regarding callback functions.

    Can you please give an example for the Asynchronous call and callback?

    one more doubt i have
    Is it always necessary to involve a Timer event for Callback function call??


    Thanks and Regards....
    Bhupesh

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Asynchronous callbacks are called that because there isn't a direct path from the code setting up the callback and the called function. You could for example have a callback when the mouse is moved (e.g. when the mouse enters a certain region) - there is obviously no correlation between the hand moving the mouse and the code running in the machine. You need some form of event to make a asynchronous callback, that forms the trigger for the callback. Exactly what this event is, depends on the system and the functions within the OS for example, supporting callbacks.

    A "synchronous" callback is where the call is more directly connected to the callback, such that a function is called with a callback as an argument, and as part of the called function, the callback is executed, so there is a direct path in the code from the call of the first function, all the way to the callback.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Have a look at working syntax and working process of how to use qsort or perhaps write a small program using qsort you will get an idea of how callback function works. Here is a small tutorial on that Callback function Ex:qsort

    ssharish

  6. #6
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    The Callback function Ex:qsort link doesn't work.

  7. #7
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101
    Easy fix

    Ye I think having a look at some examples will give you a better understanding. Qsort basically lets you sort data to the way you want (ascending,descending etc) by letting you pass your own function to it.
    Last edited by Tommo; 10-15-2007 at 05:30 AM.

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by BobS0327 View Post
    The Callback function Ex:qsort link doesn't work.
    Here we go http://www.newty.de/fpt/callback.html#chapter3

    ssharish

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    22

    Lightbulb Callback function

    Hi Guys ....
    Thanks for the replies...

    Now i got the concept,
    its used for asynchronous calls, like asynchronous data transfer.

    suppose we have to do one operation as according to a particular buffer value, which is filled through a server response.

    so we can make the next operation needed as a callback function, associated with that particular buffer.

    so what will happen whenever that buffer will filled - up, Control will automatically go to the same function(Callback).



    like

    Code:
    network_operation( )
    {
       ---------------some stmts---
    
        callback_functionreg( );   //registry of this callback can be done through
                                               //with a timer also associated with that buffer pointer
    
       ---------------some stmts---
    
    }

    Thanks Bhupesh

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 08-24-2008, 11:39 AM
  2. Callback Functions
    By valaris in forum C Programming
    Replies: 11
    Last Post: 07-31-2008, 09:20 PM
  3. Replies: 3
    Last Post: 07-19-2008, 03:12 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Callback functions
    By linuxdude in forum C Programming
    Replies: 2
    Last Post: 01-03-2004, 06:23 PM