标签:线程
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