Callback

How do you actually implement a callback from scratch in C++? I use lots of callbacks through ROS, but I have not reasoned through how callbacks are implemented.

I know you pass in a Function Pointer, but under the hood, what happens?

Callbacks are generally used in asynchronous communication scenarios, where you don’t want a thread to be blocked.

In C

I was asked this for my Apple interview. I wasn’t familiar with the syntax of a C-style callback

Here is an example of callbacks in C.

Let’s say you want to write some code that allows registering callbacks to be called when some event occurs.

First define the type of function used for the callback:

typedef void (*event_cb_t)(const struct event *evt, void *userdata);

Now, define a function that is used to register a callback:

int event_cb_register(event_cb_t cb, void *userdata);

This is what code would look like that registers a callback:

static void my_event_cb(const struct event *evt, void *data)
{
    /* do stuff and things with the event */
}
 
...
   event_cb_register(my_event_cb, &my_custom_data);
...

In the internals of the event dispatcher, the callback may be stored in a struct that looks something like this:

struct event_cb {
    event_cb_t cb;
    void *data;
};

This is what the code looks like that executes a callback.

struct event_cb *callback;
 
...
 
/* Get the event_cb that you want to execute */
 
callback->cb(event, callback->data);

In C++