ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

回调函数的具体使用

2022-09-15 14:30:29  阅读:232  来源: 互联网

标签:include 函数 thread callback 具体 pthread 回调 data


参考和摘录(https://blog.csdn.net/weixin_42339542/article/details/111701908)  非本人所写,仅用于记录和回看

 

什么是回调函数?

通俗来说,回调函数就是开发者A定义,另一个开发者B实现,并在A实现的函数中调用的函数

假如模块A的数据如何传给另一个模块B,模块B如何处理数据,模块A是不关心的,这就需要模块B将自己的处理函数给模块A,这就是所谓的函数注册。这个注册的函数是回调注册函数,被注册的函数就是回调函数。

 

回调函数实现方式就是将函数的指针作为注册函数的参数注册给调用者,调用者使用函数指针,一般为全局函数指针,接收该回调函数。这个过程一般是注册回调的过程。调用者在没有接收这个回调函数之前,直接先使用全局指针完成函数调用,所以说代码运行之前,必须要完成函数的注册,不然找不到该函数,会导致空指针错误。

 

 

例子:假如要完成温湿度采集和处理,如何实现? 考虑到模块化设计和分工,甲完成温湿度的采集,乙完成湿度的采集,丙处理这两个数据。丁负责写整个应用将三个部分串起来。其中甲乙的数据就可以使用回调函数传输给丙处理。开发之前,甲乙需要定义各种头文件里面提供自己的函数声明和回调函数的声明,以及回调函数的注册方法。

甲:负责temperature采集

乙:负责humidity采集

丙:负责数据处理

丁:负责应用程序的开发

在linux环境下使用多线程的方式模拟,线程之前使用管道通信实现。

甲的头文件

1 #ifndef _DRIVE_TMP_H__
2 #define _DRIVE_TMP_H__
3 typedef int (*callback_fuction)(int a,char *data); //函数指针定义
4 int register_callback_func_tmp(callback_fuction callback);
5 void start_tem_drive(pthread_t *thread);
6 #endif

甲的C文件

(这里用了一个全局变量指针   g_callback)

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <unistd.h>
 4 #include <pthread.h>
 5 #include "temperature_drive.h"
 6 pthread_mutex_t mutex;
 7 callback_fuction g_callback;
 8 //注册回调函数
 9 int register_callback_func_tmp(callback_fuction callback)
10 {
11     g_callback = callback;
12 }
13  
14  
15 void* callback_thread_tem(void *p1)
16 {
17     while(1)
18     {
19         int static num = 0;
20         if(NULL == g_callback)
21         {
22             printf("WAITE REGIST! \n");
23             exit(0);
24         }
25          sleep(2);
26          pthread_mutex_lock(&mutex);
27          g_callback(num,"temperature");//调用回调函数将数据返回到回调函数的实现方,也就是丙
28          num++;
29          pthread_mutex_unlock(&mutex);
30     }
31 }
32  
33 void start_tem_drive(pthread_t *thread)
34 {
35     //创建线程
36     pthread_t pthread;
37  
38     pthread_create(&pthread,NULL,callback_thread_tem,NULL);
39     
40     *thread = pthread;
41  
42     //pthread_join(pthread,NULL);
43     sleep(1);
44 }

乙的C文件

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <unistd.h>
 4 #include <pthread.h>
 5 #include "humidity_drive.h"
 6 extern pthread_mutex_t mutex;
 7 callback_fuction_hum g_callback_humidity;
 8 //注册回调函数
 9 int register_callback_func_hum(callback_fuction_hum callback)
10 {
11     g_callback_humidity = callback;
12 }
13  
14  
15 void* callback_thread_hum(void *p1)
16 {
17     while(1)
18     {
19         if(NULL == g_callback_humidity)
20         {
21             printf("WAITE REGIST! \n");
22             exit(0);
23         }
24          sleep(3);
25          pthread_mutex_lock(&mutex);
26          g_callback_humidity("humidity");//通过回调函数将数据返回
27          pthread_mutex_unlock(&mutex);
28     }
29 }
30  
31 void start_hum_drive(pthread_t *thread)
32 {
33     //创建线程
34     pthread_t pthread;
35  
36     pthread_create(&pthread,NULL,callback_thread_hum,NULL);
37     *thread = pthread;
38  
39     //pthread_join(pthread,NULL);
40     sleep(1);
41 }

 

 

 

乙同学:

1 #ifndef _HUMIDITY_H__
2 #define _HUMIDITY_H__
3  
4 typedef int (*callback_fuction_hum)(char *data); //函数指针定义
5 int register_callback_func_hum(callback_fuction_hum callback);
6 void start_hum_drive(pthread_t *thread);
7 #endif

 

 

丙同学

1 #ifndef _DATA_HANDLE_H__
2 #define _DATA_HANDLE_H__
3 void data_handle_init(void);
4 void start_data_handle(pthread_t *thread);
5 #endif

C文件

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <unistd.h>
 4 #include <pthread.h>
 5 #include "data_handle.h"
 6 #include "temperature_drive.h"
 7 #include "humidity_drive.h"
 8 //注册回调
 9 int pip_ios_fd[2] ;
10 int handle_tem_data_callback(int num, char *data)  //实现具体温度处理的回调函数
11 {
12     printf("handle_tem_data_callback\n");
13     int wret = write(pip_ios_fd[1], data, sizeof(data));
14     printf("num%d\n",num);
15     return 0;
16     
17  
18 }
19 int handle_hum_data_callback(char *data) //实现具体的湿度处理的回调函数
20 {
21     printf("handle_hum_data_callback\n");
22     int wret = write(pip_ios_fd[1], data, sizeof(data));
23 }
24  
25  
26 void* data_handle_thread(void *p1)
27 {
28  
29     while(1)
30     {
31         char data[10];
32         int ret = read(pip_ios_fd[0], data, sizeof(data));//管道为空阻塞
33         printf("rev data len:%d,data is:%s\n",ret,data);//集中处理所有收到的数据
34     
35     }
36 }
37 void data_handle_init()
38 {
39     
40     register_callback_func_tmp(handle_tem_data_callback);//调用注册回调函数
41     register_callback_func_hum(handle_hum_data_callback);
42 }
43 void start_data_handle(pthread_t *thread)
44 {
45     //创建线程
46     int pip_ret = pipe(pip_ios_fd);
47     
48     pthread_t pthread;
49  
50     pthread_create(&pthread,NULL,data_handle_thread,NULL);
51  
52     //pthread_join(pthread,NULL);
53     *thread = pthread;
54     sleep(1);
55 }

 

丁同学

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <unistd.h>
 4 #include <pthread.h>
 5 #include "temperature_drive.h"
 6 #include "humidity_drive.h"
 7 #include "data_handle.h"
 8  
 9 int main()
10 {
11     /*先调用初始化,为了先注册回调函数*/
12     pthread_t thread,thread1,thread2;
13     data_handle_init();
14     /*启动温度检测*/
15     start_tem_drive(&thread);
16     /*启动湿度检测*/
17     start_hum_drive(&thread1);
18     /*启动数据处理*/
19     start_data_handle(&thread2);
20     pthread_join(thread,NULL);
21     pthread_join(thread1,NULL);
22     pthread_join(thread2,NULL);
23     return 0;
24 }

 

 

makefile

 1 UGW_INC += -I $(pwd) ./
 2 CC = gcc
 3 UGW_SRCS = $(wildcard *.c)
 4 UGW_OBJS = $(patsubst %.c,%.o, $(UGW_SRCS))
 5  
 6 all: $(UGW_OBJS)
 7 %.o:%.c
 8     $(CC) -c  -o $@ -MD $< $(UGW_INC)
 9  
10 clean:
11     -rm -f *.o
12     -rm -f *.d

 

标签:include,函数,thread,callback,具体,pthread,回调,data
来源: https://www.cnblogs.com/donxiao-999/p/16474992.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有