回调函的作用/用途/使用场景

2014-11-24 07:32:08 · 作者: · 浏览: 0

回调函数的使用方法:

#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        pthread_mutex_t mutex; pthread_cond_t cond; typedef void (*callback_one)(void*); typedef int (*callback_two)(void *,void *); void signal_alarm(int sig) { pthread_mutex_lock(&mutex); pthread_cond_wait(&cond,&mutex); printf("I in sigane \r\n"); pthread_mutex_unlock(&mutex); } void print(callback_one f,void* para) { signal(SIGALRM,signal_alarm); alarm(8); f(para); } int main() { int a=4; int b=6; int i; char buf[8]; char temp; FILE *fp=NULL; pthread_mutex_init(&mutex,NULL); pthread_cond_init(&cond,NULL); printf("In callback !\r\n"); print(say_hello,"fdsafd"); 
       
       //printf("Out callback !\r\n");
       pthread_mutex_lock(&mutex);
       pthread_mutex_unlock(&mutex);
       pthread_cond_signal(&cond);
       printf("Out callback !\r\n");
       sleep(10);

	return 0;
}
 
       

 
       

总感觉回调函数在实际应用中没有多大的用途,感觉和顺序执行没有什么区别,在下面的几种情况使用回调还是可以很容易发现回调的使用价值的:

1、在必须给别的函数提供接口的时候

2、在需要定时操作,或者条件操作的时候(上面的例子包含了这两种情况的用法)