首页 > 编程语言 > 详细

多线程

时间:2016-04-16 19:37:08      阅读:123      评论:0      收藏:0      [点我收藏+]


 1 #include<stdio.h>
  2 #include<pthread.h>
  3 #include<stdlib.h>
  4 void* thread(void* arg)
  5 {
  6     int count =5;
  7     while(count--)
  8     {
  9         printf("this is thread,%u\n",pthread_self());
 10         sleep(1);
 11     }
 12     //return (void*)3;
 13     //pthread_exit((void*)3);
 14     //pthread_cancel(pthread_self());
 15 }
 16 int main()
 17 {
 18     pthread_t tid;
 19     int ret = pthread_create(&tid,NULL,thread,NULL);
 20     if(ret!=0)
 21     {
 22         return -1;
 23     }
  24     int count =10;
 25     while(count--)
 26     {
 27         printf("this is main,%u\n",pthread_self());
 28         sleep(1);
 29     }
 30 
 31     pthread_cancel(tid);
 32     void *retval;
 33     ret = pthread_join(tid,&retval);
 34     if(ret==0)
 35     {
 36         printf("pthread_join secced,%d\n",(int)retval);
 37     }
 38     return 0;
 39 }
 
 [fbl@localhost thread]$ ./creat
this is main,3077727936
this is thread,3077725040
this is main,3077727936
this is thread,3077725040
this is main,3077727936
this is thread,3077725040
this is main,3077727936
this is thread,3077725040
this is main,3077727936
this is thread,3077725040
this is main,3077727936
this is main,3077727936
this is main,3077727936
this is main,3077727936
this is main,3077727936
pthread_join secced,0


多线程

原文:http://fengbaoli.blog.51cto.com/10538178/1764518

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!