Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call back routine

Tags:

c++

opencv

In the Learning OpenCV book, I came to the term callback, and sometimes used with routine as callback routine.

What do we mean when we saycallback?

Thanks.

like image 827
Simplicity Avatar asked Jul 24 '11 14:07

Simplicity


4 Answers

What is a Callback function?
In simple terms, a Callback function is a function that is not called explicitly by the programmer. Instead, there is some mechanism that continually waits for events to occur, and it will call selected functions in response to particular events.
This mechanism is typically used when an operation(function) takes a long time for execution and the caller of the function does not want to wait till the operation is complete, but does wish to be intimated of the outcome of the operation. Typically, Callback functions help implement such an asynchronous mechanism, wherein the caller registers to get inimated about the result of the time consuming processing and continuous other operations while at a later point of time, the caller gets informed of the result.

A practical example:
Windows event processing:
virtually all windows programs set up an event loop, that makes the program respond to particular events (e.g button presses, selecting a check box, window getting focus) by calling a function. The handy thing is that the programmer can specify what function gets called when (say) a particular button is pressed, even though it is not possible to specify when the button will be pressed. The function that is called is referred to as a callback.

A source Code Illustration:

//warning:  Mind compiled code, intended to illustrate the mechanism    
#include <map>

typedef void (*Callback)();
std::map<int, Callback>  callback_map;

void RegisterCallback(int event, Callback function)
{
    callback_map[event] = function;
}

bool finished = false;

int GetNextEvent()
{
    static int i = 0;
    ++i;
    if (i == 5) finished = false;
}

void EventProcessor()
{
    int event;
    while (!finished)
    {
        event = GetNextEvent();
        std::map<int, Callback>::const_iterator it = callback_map.find(event);
        if (it != callback_map.end())    // if a callback is registered for event
        {
            Callback function = *it;
            if (function)   
            {
                (*function)();
            }
            else
            {
               std::cout << "No callback found\n";
            }
        }
    }
}

void Cat()
{
   std::cout << "Cat\n";
}

void Dog()
{
    std::cout << "Dog\n";
}

void Bird()
{
    std::cout << "Bird\n";
}

int main()
{
    RegisterCallBack(1, Cat);
    RegisterCallback(2, Dog);
    RegisterCallback(3, Cat);
    RegisterCallback(4, Bird);
    RegisterCallback(5, Cat);

    EventProcessor(); 
    return 0;
}

The above would output the following:

Cat  
Dog   
Cat  
Bird  
Cat  

Hope this helps!


Note:
Imported this answer from one of my old answers here.

like image 52
Alok Save Avatar answered Nov 19 '22 14:11

Alok Save


Following the Holywood principle "Don't call us, we call you", a callback is a reference to a function which is passed to another function.

The callback will be called by the function it is given to for instance when data is available or certain processing steps need to be performed.

like image 42
Christopher Oezbek Avatar answered Sep 19 '22 17:09

Christopher Oezbek


"I don't call it by myself, but the system (or some others) will call it". That's callback.

like image 9
Stan Avatar answered Nov 19 '22 13:11

Stan


They mean that you pass a pointer to a procedure to OpenCV. This will be called back when something happens. This can e.g. seen at cvSetMouseCallback(). The function referenced by the pointer will be called whenever the mouse moves.

like image 3
marc Avatar answered Nov 19 '22 13:11

marc