This article mainly explains that if the callback is implemented, especially in the encapsulation interface, callbacks are particularly important, we first assume that there are two programmers writing code, a programmer to write the underlying driver interface, b programmer to write the upper-level application, but at this time the underlying drive interface A has a data d need to transfer to B, there are two ways:
1, a The data d is stored well in the interface function, b when they want to read when the time to read, this is what we often use the function call, when the initiative is B.
2, a implementation callback mechanism, when the data changes will notify B, you can read the data, and then B in the user layer of the callback function read Speed D, complete OK. At this point the initiative is a.
Obviously the first method is too inefficient and b doesn't know when to call the interface function to read data d. And the second way because B's read data operation is dependent on a, only a is called B-read data, then B to read the data. The interrupt read is also implemented.
So how is the callback implemented, in fact the callback function is a function called through the function pointer. If user layer B passes the pointer (address) of the function as an argument to the underlying drive A, when the pointer is used in a to invoke the function it points to, we say this is a callback function.
Note : is called in a, see here, although the function is in B, but B does not call the function itself, but the function of the functions of the pointer through a interface function is passed from a, a to manipulate execution, this is the meaning of the callback.
Here's an example to illustrate
First write a programmer's code
//-----------------------bottom-level implementation-----------------------------typedef void(*PCB) (inta);//function pointer definition, can be used directly behind the PCB, convenienttypedef structparameter{intA PCB callback;} Parametervoid* Callback_thread (void*P1)//A thread is used here{//do somethingparameter* p = (parameter*) p1; while(1) {printf("Getcallback print! \ n "); Sleep3);//Delay 3 seconds to execute callback functionP->callback (P->a);//function pointer execution function, this function is from application layer B}}//Leave the interface function for application layer BexternSetcallbackfun (intA, PCB callback) {printf("Setcallbackfun print! \ n "); Parameter *p =malloc(sizeof(parameter)) ; P->a =Ten; P->callback = callback;//Create threadpthread_t thing1; Pthread_create (&thing1,null,callback_thread, (void*) p); Pthread_join (thing1,null);}
The above code is the bottom interface programmer a write all the code, set aside the interface function Setcallbackfun can
Following the implementation of Application B's program, B is responsible for invoking the Setcallbackfun function, and adding a function, and the function pointer eating function through the setcallbackfun (int A, PCB callback) the second parameter PCB callback pass down.
//-----------------------应用者-------------------------------void fCallBack(int a) // 应用者增加的函数,此函数会在A中被执行{ //do something printf("a = %d\n",a); printf("fCallBack print! \n");}int main(void){ SetCallBackFun(4,fCallBack); return0;}
Running the program will see
先会打印A程序的 printf("GetCallBack print! \n");然后等待3秒钟才会打印应用者B的 printf("fCallBack print! \n");
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C-language asynchronous callbacks