回調(diào)函數(shù)其實(shí)就是函數(shù)指針的簡(jiǎn)單使用,目的在于讓代碼更具有模塊化的思想。
1、基礎(chǔ)知識(shí)
所謂回調(diào),就是模塊1要通過(guò)模塊2的完成一定的功能,但是模塊2自己無(wú)法實(shí)現(xiàn)全部功能,需要反過(guò)頭來(lái)調(diào)用模塊A中的某個(gè)函數(shù)來(lái)完成,那么這個(gè)函數(shù)就是回調(diào)函數(shù)。
2、各自工作范疇
在模塊1中需要完成:
a.約定接口規(guī)范,也就是定義回調(diào)函數(shù)的函數(shù)原型
typedef void (*pfn)(void xxx);
就是一個(gè)函數(shù)指針類(lèi)型.
b.定義處理函數(shù),類(lèi)型與上面函數(shù)指針一致
void msg_proc(void xxx);
c.定義回調(diào)函數(shù)的注冊(cè)函數(shù)
void register(pfn *xxx, pfn*yyy);
就是將上面定義的函數(shù)賦給模塊2中定義的函數(shù)指針變量,這樣模塊2就可以使用上面定義的實(shí)際處理的函數(shù)了.
模塊2中,只要要定義一個(gè)回調(diào)函數(shù)的函數(shù)原型的變量,然后調(diào)用回調(diào)函數(shù)的注冊(cè)函數(shù),完成初始化,就可以使用這個(gè)變量,調(diào)用
2、舉例
下面是設(shè)計(jì)了兩個(gè)線程,分別稱為模塊1和模塊2,模塊1產(chǎn)生數(shù)據(jù)data為1,模塊2則是處理數(shù)據(jù)使data為0,模塊1和模塊2的實(shí)現(xiàn)可參考上面工作范疇的內(nèi)容。
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <pthread.h>
#include <semaphore.h>
pthread_t tid1;
pthread_t tid2;
sem_t sem;
int data = 0;
void sig_handler(int sig)
{
if(sig == SIGINT){
if(pthread_cancel(tid1) != 0){
perror("thread cancel fail");
exit(0);
}
if(pthread_cancel(tid2) != 0){
perror("thread cancel fail");
exit(0);
}
printf("\n\n 兩個(gè)線程取消\n");
}
}
/*module 1*/
typedef void (*pfn_callback_t)(int* para);
void data_pro(int* para)
{
*para = 0;
}
void fun_callback_register(pfn_callback_t* fun)
{
*fun = data_pro;
}
void* thread_fun1(void *arg)
{
signal(SIGINT, sig_handler);
if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL) != 0){
perror("pthread set cancel state fail");
pthread_exit(NULL);
exit(0);
}
if(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL) != 0){
perror("pthread set cancel type fail");
pthread_exit(NULL);
exit(0);
}
while(1){
data = 1;
printf("thread-1:%d\n",data);
sem_post(&sem);
sleep(2);
}
}
/*module 2*/
pfn_callback_t myfun;
void* thread_fun2(void *arg)
{
signal(SIGINT, sig_handler);
if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL) != 0){
perror("pthread set cancel state fail");
pthread_exit(NULL);
exit(0);
}
if(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL) != 0){
perror("pthread set cancel type fail");
pthread_exit(NULL);
exit(0);
}
//init module 2, use module 1 register function
fun_callback_register(&myfun);
while(1){
sem_wait(&sem);
myfun(&data);
printf("thread-2:%d\n",data);
}
}
/*main module*/
int main(int argc, char const *argv[])
{
signal(SIGINT, sig_handler);
int ret;
if( sem_init(&sem,0,0) != 0 ) {
printf("init sem error\n");
return 0;
}
//create two thread
if((ret = pthread_create(&tid1,NULL,thread_fun1,NULL)) != 0){
perror("thread create fail");
return -1;
}
if((ret = pthread_create(&tid2,NULL,thread_fun2,NULL)) != 0){
perror("thread create fail");
return -1;
}
//wait thread exit
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
printf("\n\n退出程序\n");
return 0;
}
結(jié)果為:
thread-1:1
thread-2:0
thread-1:1
thread-2:0
符合預(yù)期