linux线程(创建,等待,终止)

linux线程创建等待终止

  1. /* FirstThreadFunc.c*/  
  2. #include <stdio.h>  
  3. //#include <unistd.h>  
  4. //#include <stdlib.h>  
  5. #include <pthread.h>  
  6. void  
  7. thread (void)  
  8. {  
  9. //  sleep(1);  
  10.   int i;  
  11.   int tid = pthread_self();//返回自己的线程ID  
  12.   for (i = 0; i < 3; i++)  
  13.     printf ("This is a pthread\n");  
  14.   pthread_exit("hello world!!\n");//把字符串的首地址传给thread_result  
  15. }  
  16.   
  17. int  
  18. main (void)  
  19. {  
  20.   void *thread_result;  
  21.   pthread_t id;  
  22.   int i, res;  
  23.   res = pthread_create(&id, NULL, (void *) thread, NULL);//创建线程  
  24.   pthread_join(id, &thread_result);//等待id的线程结束  
  25.   if(res != 0)  
  26.     {  
  27.       printf ("Create pthread error!\n");  
  28.       exit (1);  
  29.     }  
  30. //    sleep(1);  
  31.   for (i = 0; i < 3; i++)  
  32.     printf ("This is the main process\n");  
  33.   
  34.   printf("thread joined ,it returned %s\n",(char *)thread_result);  
  35.   return (0);  
  36. }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值