Linux 线程(创建/等待/终止)

来源:互联网 发布:mysql 一天内的数据 编辑:程序博客网 时间:2024/05/16 01:47

http://blog.csdn.net/hubi0952/article/details/7982487

[cpp] view plaincopyprint?
  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);  


0 0
原创粉丝点击