LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 8716|回复: 3

[求助]pthread_exit和pthread_join的用法

[复制链接]
发表于 2004-11-29 09:26:44 | 显示全部楼层 |阅读模式
我在编一个小程序,想用pthread_exit结束一个线程,返回一个值,并用pthread_join接收这个值,打印出来。是不是pthrad_exit(*)只能返回一个整数值?帮助里也没有说啊。源程序如下:

  1. /*example.c*/                                               
  2. #include <stdio.h>                                                            
  3.                                                                               
  4. #include <pthread.h>                                                           
  5.                                                                               
  6. void thread1(char s[])                                                         
  7.                                                                               
  8. {                                                                                                                                                                                               
  9. /*b1[]="This is the thread1 return value!";返回值,原来放在这里,后来想到,还是应该放到主函数里。*/                       
  10. printf("This is a pthread1.\n");                                               
  11. printf("%s\n",s);                                                              
  12. pthread_exit(b1);  //结束线程,返回一个值。                                                            
  13. }                                                                              
  14. void thread2(char s[])                                                         
  15.                                                                               
  16. {                                                                              
  17. /*b2[]="This is the thread2 return value!";同上*/                        
  18. printf("This is a pthread2.\n");                                               
  19. printf("%s\n",s);                                                              
  20. pthread_exit(b2);                                                               
  21. }                                                                              

  22. /**************main function ****************/                                                                          
  23. int main(void)                                                                 
  24.                                                                               
  25. {                                                                              
  26.                                                                               
  27. pthread_t id1,id2;                                                            
  28.                                                                               
  29. int i,ret1,ret2;   
  30. char b1[]="This is the thread1 return value!";//线程1结束时的返回值
  31. char b2[]="This is the thread2 return value!";//线程2结束时的返回值                                                         
  32. char a1[50],s1[]="This is first thread!";                                      
  33. char a2[50],s2[]="This is second thread!";                                    
  34. for(i=0;i<50;i++)                                                              
  35. {                                                                              
  36. a1[i]='\0';                                                                    
  37. a2[i]='\0';                                                                    
  38. }                                                                              
  39. ret1=pthread_create(&id1,NULL,(void *) thread1,s1);                           
  40. ret2=pthread_create(&id2,NULL,(void *) thread2,s2);                           
  41. if(ret1!=0){                                                                  
  42.                                                                               
  43. printf ("Create pthread1 error!\n");                                          
  44.                                                                               
  45. exit (1);                                                                     
  46.                                                                               
  47. }                                                                              
  48. pthread_join(id1,a1);                                                         
  49. printf("%s\n",a1);                                                            
  50. if(ret2!=0){                                                                  
  51.                                                                               
  52. printf ("Create pthread2 error!\n");                                          
  53.                                                                               
  54. exit (1);                                                                     
  55.                                                                               
  56. }                                                                              
  57.                                                                               
  58. printf("This is the  main process.\n");                                       
  59.                                                                               
  60. pthread_join(id2,a2);                                                         
  61. printf("%s\n",a2);                                                            
  62.                                                                               
  63.                                                                               
  64. return (0);                                                                    
  65.                                                                               
  66. }  
复制代码

运行如下:                                                                           
  [*****@***** c]$ gcc -g -lpthread -o example example.c
example.c: In function `thread1':
example.c:13: `b1' undeclared (first use in this function)
example.c:13: (Each undeclared identifier is reported only once
example.c:13: for each function it appears in.)
example.c: In function `thread2':
example.c:21: `b2' undeclared (first use in this function)
example.c: In function `main':
example.c:50: warning: passing arg 2 of `pthread_join' from incompatible pointer
type
example.c:62: warning: passing arg 2 of `pthread_join' from incompatible pointer
type            
我不明白的地方:b1[],b2[] 都是主函数里定义的的,;两个线程函数里不能用吗?而且b1[],b2[]放到各个线程函数里,后面两个警告也是会出现的,打印也不对。请教各位大虾了。
 楼主| 发表于 2004-11-29 10:10:07 | 显示全部楼层
修改如下后运行正确:
  1.                        
  2. /* example.c*/

  3. #include <stdio.h>

  4. #include <pthread.h>

  5. void thread1(char s[])


  6. {
  7.         printf("This is a pthread1.\n");
  8.         printf("%s\n",s);
  9.         pthread_exit("Hello first!");  //结束线程,返回一个值。
  10. }
  11. void thread2(char s[])


  12. {
  13.         printf("This is a pthread2.\n");

  14.         printf("%s\n",s);

  15.         pthread_exit("Hello second!");

  16. }

  17. /**************main function ****************/

  18. int main(void)


  19. {
  20.         pthread_t id1,id2;
  21.         void *a1,*a2;
  22.         int i,ret1,ret2;
  23.         char s1[]="This is first thread!";
  24.         char s2[]="This is second thread!";
  25.         ret1=pthread_create(&id1,NULL,(void *) thread1,s1);

  26.         ret2=pthread_create(&id2,NULL,(void *) thread2,s2);

  27.         if(ret1!=0){
  28.                 printf ("Create pthread1 error!\n");
  29.                 exit (1);
  30.         }
  31.         pthread_join(id1,&a1);

  32.         printf("%s\n",(char*)a1);

  33.         if(ret2!=0){
  34.                 printf ("Create pthread2 error!\n");
  35.                 exit (1);
  36.         }
  37.         printf("This is the  main process.\n");
  38.         pthread_join(id2,&a2);
  39.         printf("%s\n",(char*)a2);
  40.         return (0);

  41. }  
复制代码

运行结果:
[****@XD**** c]$ ./example
This is a pthread1.
This is first thread!
Hello first!
This is the  main process.
This is a pthread2.
This is second thread!
Hello second!
看来,是我在线程函数里的变量设得有问题。
发表于 2004-11-29 20:19:33 | 显示全部楼层
啊,不是编译吗,为什么说运行如下
 楼主| 发表于 2004-11-30 14:21:25 | 显示全部楼层
最初由 x11 发表
啊,不是编译吗,为什么说运行如下

不好意思,说错了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表