Linux C 主线成与子线程参数传递


Linux C 主线成与子线程参数传递

  1. #include <stdio.h>   
  2. #include <pthread.h>   
  3.   
  4. void* fun(void* arg)  
  5. {  
  6.    
  7.    
  8.  printf("======[%d]====\n",(int)arg);    
  9.  pthread_exit((void*)22);  
  10. }  
  11.   
  12. int main(void)  
  13. {  
  14.   
  15.  pthread_t tid;   
  16.   
  17.  pthread_create(&tid,NULL,fun,(void*)99);    //你没注意的东东   
  18.  void *val;  
  19.  pthread_join(tid,&val);  
  20.  printf("------[%d]-----\n",(int)val);  
  21.   
  22.   
  23.  return 0;  
  24. }  

第二种传结构体    

  1. #include <stdio.h>   
  2. #include <pthread.h>   
  3. #include <stdlib.h>   
  4.   
  5. typedef struct   
  6. {  
  7.     int a;  
  8.     float b;  
  9.       
  10. }*LYp,LY;  
  11.   
  12. LYp sheep;  
  13. float f=0;  
  14. void* fun(void* arg)  
  15. {  
  16.     LYp p=(LYp)arg;  
  17.     printf("===[%d]===[%f]====\n",p->a,p->b);  
  18.     f=1.234;          
  19.     pthread_exit(&f);  
  20. }  
  21.   
  22. int main(void)  
  23. {  
  24.     sheep=malloc(sizeof(LY));  
  25.     sheep->a=100;  
  26.     sheep->b=3.33;  
  27.     pthread_t tid;    
  28.   
  29.     pthread_create(&tid,NULL,fun,sheep);  
  30.     void *val;  
  31.     pthread_join(tid,&val);  
  32.     printf("------[%lf]-----\n",*((float*)val));  
  33.   
  34.     free(sheep);  
  35.   
  36.   
  37.     return 0;  
  38. }  

相关内容