The function and implementation mechanism of callback function - with example

Reprinted: http://blog.csdn.net/yangjun1115/article/details/25985827

The callback function has always been used by itself, but the mechanism of how to use it and how it is actually implemented is not very clear.

In the past two days, I deliberately checked the relevant information on the Internet, and in the attitude of learning, I strengthened my poor knowledge. (Writing is wrong, please correct me, thank you very much~)


The main function of the callback function:
1. The callback function is the callback function implemented by this module, but the call is not adjusted by itself, but by other modules
2. You can better grasp the timing of calling the function. The callback function depends on the module corresponding to the registered callback. The callback function is triggered by this module, and the triggering conditions and time points are controlled by this module.
 
 (usually triggered by the module's event message)
3. Using between different modules can separate the trigger logic from the business logic, making the code functionality clearer and easier to understand.

Note:
The callback function runs under the thread that triggers the callback. If a lot of things are done in the callback function, it is easy to cause the trigger module-owning thread to be blocked.
For multi-threaded programming, special attention should be paid to the blockage of threads, which will affect the running timing of other threads and cause some bugs

. Common usage:
In the form of a library, for others to call to achieve

the above is based on the corresponding information on the Internet, plus my own summary experience, the


following is a callback function written by myself - an example of usage:
Mainly divided into 2 modules 
Module A is the module that defines the callback function and is responsible for triggering the callback, usually the event message module
Module B is a module that implements the callback function. Generally, it is required to process the business logic module. Module

A needs to expose the definition and registration callback interface to Module B.
typedef void (*TestCB)(char testData);
void A_SetTestCb(TestCB pTestCb)

Module B implements specific callbacks The function is fine, in this case B_UserTestCbDetail(char testData)

Mainly divided into 3 steps: register callback function -> message event trigger callback function -> execute callback function

Attached code:

#include "stdio.h"

//Define the callback function - alias, which is convenient for subsequent use------ needs to be provided to module B
typedef void (*TestCB)(char testData);

//Global callback variable inside module A, used for internal binding of module A Register back to call
TestCB g_pTestCbHandle;

/************************************************ *********
Module A external function --------- needs to be provided to module B
The global callback variable defined inside module A is registered with the external module
The callback function is bound and used for the subsequent module A. After the trigger condition is met, the
Send callback
************************************************ ******/
void A_SetTestCb(TestCB pTestCb)
{
g_pTestCbHandle = pTestCb;
}

/*****************************************************
Module A internal function
Usually module A triggers the corresponding event because certain conditions are met
Thus calling the corresponding callback function
******************************************** **********/
void A_CallTestCb()
{
char testData = 100;
//Trigger call callback implementation function
g_pTestCbHandle(testData);
}

/*****************************************************
Module B internal function
Callback function implemented by module B - specific operation
******************************************** *************/
void B_UserTestCbDetail(char testData)
{
printf("step 3\n");
printf("[do] call-back\n");

//Implementation
printf("UserTestCbDetail, call back is achieve! testData = %d\n", testData);
}

void main()
{
printf("step 1\n");
printf("[register] call-back\n");
A_SetTestCb(B_UserTestCbDetail);

printf("step 2\n");
printf("[call] call-back\n");
A_CallTestCb();
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325466688&siteId=291194637