How to implement the callback function in C?
Callback in programming Implementing callback function in C Learn C Programming
What is the callback function?
A callback function is a function you provide to another function, allowing it to be called by another function.
Callbacks are most easily described in terms of the telephone system. A function call is analogous to calling someone on a telephone, asking her a question, getting an answer, and hanging up; adding a callback changes the analogy so that after asking her a question, you also give her your name and number so she can call you back with the answer. -- Paul Jakubik, "Callback Implementations in C++"
The callback is highly used in the event-driven design where the asynchronous notification can be sent to another process. For example, you need a notification whenever a USB is inserted into the system or removed from the system without blocking your current task. You want to enable the copy operation if the USB is plugged in or you want to stop the copy operation and notify the user if the USB is removed from the system. In order to achieve the above design one can write the main function for copy operation and error notification to the user and same time, a thread can be created which will keep on checking the status of the USB. In the thread, upon ADD/REMOVE event of USB, it can call the callback function which will allow the main process to do the desired operation.
At the system level, a signal handler works with the concept of callback functions.
Let’s understand this one simple example. Notify the main process whenever a number is completely divisible by 10.