您的位置:首页 > 运维架构 > Linux

Linux回调函数

2016-03-31 16:51 435 查看
如果参数是一个函数指针,调用者可以传递一个函数的地址给实现者,让实现者去调用它,这称为回调函数(Callback Function)。例如
qsort(3)
bsearch(3)


表 24.7. 回调函数示例:
void func(void (*f)(void *), void *p);


调用者实现者
提供一个回调函数,再提供一个准备传给回调函数的参数。

把回调函数传给参数
f
,把准备传给回调函数的参数按
void *
类型传给参数
p


在适当的时候根据调用者传来的函数指针
f
调用回调函数,将调用者传来的参数
p
转交给回调函数,即调用
f(p);


以下是一个简单的例子。实现了一个
repeat_three_times
函数,可以把调用者传来的任何回调函数连续执行三次。

例 24.7. 回调函数
/* para_callback.h */
#ifndef PARA_CALLBACK_H
#define PARA_CALLBACK_H

typedef void (*callback_t)(void *);
extern void repeat_three_times(callback_t, void *);

#endif
/* para_callback.c */
#include "para_callback.h"

void repeat_three_times(callback_t f, void *para)
{
f(para);
f(para);
f(para);
}
/* main.c */
#include <stdio.h>
#include "para_callback.h"

void say_hello(void *str)
{
printf("Hello %s/n", (const char *)str);
}

void count_numbers(void *num)
{
int i;
for(i=1; i<=(int)num; i++)
printf("%d ", i);
putchar('/n');
}

int main(void)
{
repeat_three_times(say_hello, "Guys");
repeat_three_times(count_numbers, (void *)4);
return 0;
}


回顾一下前面几节的例子,参数类型都是由实现者规定的。而本例中回调函数的参数按什么类型解释由调用者规定,对于实现者来说就是一个
void *
指针,实现者只负责将这个指针转交给回调函数,而不关心它到底指向什么数据类型。调用者知道自己传的参数是
char *
型的,那么在自己提供的回调函数中就应该知道参数要转换成
char *
型来解释。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: