From the reading that I have done, Core Audio relies heavily on callbacks (and C++, but that's another story).
I understand the concept (sort of) of setting up a function that is called by another function repeatedly to accomplish a task. I just don't understand how they get set up and how they actually work. Any examples would be appreciated.
Callbacks in C are usually implemented using function pointers and an associated data pointer. You pass your function
on_event()
and data pointers to a framework functionwatch_events()
(for example). When an event happens, your function is called with your data and some event-specific data.Callbacks are also used in GUI programming. The GTK+ tutorial has a nice section on the theory of signals and callbacks.
There is no "callback" in C - not more than any other generic programming concept.
They're implemented using function pointers. Here's an example:
Here, the
populate_array
function takes a function pointer as its third parameter, and calls it to get the values to populate the array with. We've written the callbackgetNextRandomValue
, which returns a random-ish value, and passed a pointer to it topopulate_array
.populate_array
will call our callback function 10 times and assign the returned values to the elements in the given array.A simple call back program. Hope it answers your question.
A callback function in C is the equivalent of a function parameter / variable assigned to be used within another function.Wiki Example
In the code below,
The function (*numberSource) inside the function call PrintTwoNumbers is a function to "call back" / execute from inside PrintTwoNumbers as dictated by the code as it runs.
So if you had something like a pthread function you could assign another function to run inside the loop from its instantiation.
A callback in C is a function that is provided to another function to "call back to" at some point when the other function is doing its task.
There are two ways that a callback is used: synchronous callback and asynchronous callback. A synchronous callback is provided to another function which is going to do some task and then return to the caller with the task completed. An asynchronous callback is provided to another function which is going to start a task and then return to the caller with the task possibly not completed.
A synchronous callback is typically used to provide a delegate to another function to which the other function delegates some step of the task. Classic examples of this delegation are the functions
bsearch()
andqsort()
from the C Standard Library. Both of these functions take a callback which is used during the task the function is providing so that the type of the data being searched, in the case ofbsearch()
, or sorted, in the case ofqsort()
, does not need to be known by the function being used.For instance here is a small sample program with
bsearch()
using different comparison functions, synchronous callbacks. By allowing us to delegate the data comparison to a callback function, thebsearch()
function allows us to decide at run time what kind of comparison we want to use. This is synchronous because when thebsearch()
function returns the task is complete.An asynchronous callback is different in that when the called function to which we provide a callback returns, the task may not be completed. This type of callback is often used with asynchronous I/O in which an I/O operation is started and then when it is completed, the callback is invoked.
In the following program we create a socket to listen for TCP connection requests and when a request is received, the function doing the listening then invokes the callback function provided. This simple application can be exercised by running it in one window while using the
telnet
utility or a web browser to attempt to connect in another window.I lifted most of the WinSock code from the example Microsoft provides with the
accept()
function at https://msdn.microsoft.com/en-us/library/windows/desktop/ms737526(v=vs.85).aspxThis application starts a
listen()
on the local host, 127.0.0.1, using port 8282 so you could use eithertelnet 127.0.0.1 8282
orhttp://127.0.0.1:8282/
.This sample application was created as a console application with Visual Studio 2017 Community Edition and it is using the Microsoft WinSock version of sockets. For a Linux application the WinSock functions would need to be replaced with the Linux alternatives and the Windows threads library would use
pthreads
instead.It is lot easier to understand an idea through example. What have been told about callback function in C so far are great answers, but probably the biggest benefit of using the feature is to keep the code clean and uncluttered.
Example
The following C code implements quick sorting. The most interesting line in the code below is this one, where we can see the callback function in action:
The compare_s2b is the name of function which qsort() is using to call the function. This keeps qsort() so uncluttered (hence easier to maintain). You just call a function by name from inside another function (of course, the function prototype declaration, at the least, must precde before it can be called from another function).
The Complete Code