A little understanding of callback functions



Callback

 

When looking at LWIP , I saw the use of callback functions, and when I looked at the OPC source code of a foreigner company, I saw the use of callback functions. It is not used when looking at some codes in China (our company's software, etc.). So, I am very curious about the callback function. In the past, I used callback functions when I wrote VC programs, but I didn't use C language to use them. Recently, I saw that callback functions ( LWIP , OPC programs of two companies, etc.) are widely used in a large number of foreign classic codes , all of which are implemented in C language, not the ones used by others in VC windows programs.

In order to understand the mystery of this function, we first ask three questions:

1.        What is the callback function?

2.        How to develop and use the callback function?

3.        What is the function of the callback function and under what circumstances should it be used?

 

Study with questions, with purpose! Hehe, personal experience.

Open baidu.com , google.cn search a lot of information, as follows:

By the way, I admire a certain gentleman's signature: 1 Live well, because we will die for a long time. 25,000 years of civilization, 200 years of helplessness

 

first question:

*******************************************************************************

In fact, a callback is a process of using a function pointer to call a function .  

Why use callbacks ? For example, I want to write a submodule for you to   receive commands from remote sockets . When I receive commands ,   I need to call the functions of your main module to   perform corresponding processing . But I I don't know which function you want to use to handle this command ,   and     I don't know what your main module is .   cpp or .   function handles it ...   how ?

Use callbacks !

—— lone wolf

 

Using a callback function is actually when calling a function (usually an API function), passing the address of one of your own functions (this function is a callback function) as a parameter to that function. And when that function is needed, use the passed address to call the callback function. At this time, you can use this opportunity to process messages or complete certain operations in the callback function.

——an expert

 

The callback function is written by yourself. You need to call another function, and one of the parameters of this function is the name of your callback function. In this way, the system will call the callback function you wrote when necessary, so that you can complete what you want to do in the callback function.

- green leaves

 

http://hi.baidu.com/zhuyipeng/blog/item/863fefdb7c736c63d1164eec.html is a good article.

 

What is a callback function ?
A callback function is a function provided by an   application program to a Windows system DLL or other DLLs . It is generally used to intercept messages, obtain system information, or handle asynchronous events. The application tells the DLL the address pointer of the callback function , and the DLL will call the function when appropriate. The callback function must abide by the pre-specified parameter format and delivery method, otherwise, once the DLL calls it, the program or system will crash. Usually, the callback function adopts the calling method of the standard Windows API , that is, __stdcall . Of course, the DLL compiler can define the calling method by itself, but the client program must also abide by the same regulations. In the __stdcall mode, the parameters of the function are pushed onto the stack from right to left. Except for pointers or references, the parameters are passed by value. Before the function returns, it is responsible for popping the parameters from the stack.
  Understand callback functions !

——jufengfeng _

 

Function Pointers provide the concept of callback functions.

—— newty.de

*******************************************************************************

After reading so much information, I just summarize the definition of each in one sentence: callback function is a usage of function pointer.

In some materials, there is a lot of discussion about how the callback function is called, who is calling it, and there are many graphics. I think they have not seen the essence of the problem.

 

 

second question:

*********************************************************************

I implemented a very simple callback function.

#include <stdio.h>

 

void printWelcome(int len)

{

       printf(" Welcome welcome -- %d/n", len);

}

 

void printGoodbye(int len)

{

       printf(" Send off, drop off -- %d/n", len);

}

 

void callback(int times, void (* print)(int))

{

       int i;

       for (i = 0; i < times; ++i)

       {

              print(i);

       }

       printf("/n I don't know whether you are welcoming or seeing off !/n/n");

}

void main(void)

{

       callback(10, printWelcome);

       callback(10, printGoodbye);

       printWelcome(5);

}

*******************************************************************************

上面的代码没有被任何系统函数调用,说明那些东西只是撒撒土迷迷路人眼而已。还有面相对象编程时,用class给封装起来也是掩人耳目,不要被外表所迷惑。

 

 

第三个问题:

*********************************************************************

用过STL的人都知道,在STL中众多算法和程序都用到回调函数,这实现了一种策略。只要任何符合我的标准的函数和计算都可以用我这个公式。你可以实现各种各样的回调函数,只要符合我的格式就能用。

就上面的程序来说,你只要函数格式符合cllback第二个参数的格式不论你给别人做饭、铺床叠被都可以正常工作。这就是回调的作用,把回调实现留给别人。

这是一个用法。

 

有一位朋友用分层的概念来解释了回调机制:callback函数为B层,main函数和print*函数为A层,A层调用了B层的回调函数callmeback,而B层的回调函数调用了A层的实现函数print*。说白了B层就是一个接口。

 

 

这是我的理解。Over

Guess you like

Origin blog.csdn.net/landeli2/article/details/50474095