美文网首页
回调函数

回调函数

作者: 为瞬间停留 | 来源:发表于2018-06-22 10:23 被阅读19次

    回调函数其实就是函数指针的简单使用,目的在于让代码更具有模块化的思想。

    1、基础知识

    所谓回调,就是模块1要通过模块2的完成一定的功能,但是模块2自己无法实现全部功能,需要反过头来调用模块A中的某个函数来完成,那么这个函数就是回调函数。

    2、各自工作范畴
    在模块1中需要完成:
    a.约定接口规范,也就是定义回调函数的函数原型

    typedef void (*pfn)(void xxx);
    

    就是一个函数指针类型.

    b.定义处理函数,类型与上面函数指针一致

    void  msg_proc(void xxx);
    

    c.定义回调函数的注册函数

    void register(pfn *xxx, pfn*yyy);
    

    就是将上面定义的函数赋给模块2中定义的函数指针变量,这样模块2就可以使用上面定义的实际处理的函数了.

    模块2中,只要要定义一个回调函数的函数原型的变量,然后调用回调函数的注册函数,完成初始化,就可以使用这个变量,调用

    2、举例
    下面是设计了两个线程,分别称为模块1和模块2,模块1产生数据data为1,模块2则是处理数据使data为0,模块1和模块2的实现可参考上面工作范畴的内容。

    #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 两个线程取消\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;
    }
    

    结果为:

    thread-1:1
    thread-2:0
    thread-1:1
    thread-2:0
    

    符合预期

    相关文章

      网友评论

          本文标题:回调函数

          本文链接:https://www.haomeiwen.com/subject/cwzhyftx.html