Chinaunix首页 | 论坛 | 博客
  • 博客访问: 95079
  • 博文数量: 59
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 0
  • 用 户 组: 普通用户
  • 注册时间: 2018-11-18 23:26
文章分类
文章存档

2021年(1)

2013年(1)

2012年(57)

分类:

2012-10-08 16:27:45

一直担心在线程里加sleep会引起进程所有的线程挂起,测试过了不会
 
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <sys/time.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #define MAX 10

  7. pthread_t thread[2];
  8. void * thread1()
  9. {
  10.     while(1)printf ("thread1 : I'm thread 1\n");
  11. }
  12. void * thread2()
  13. {
  14.     printf("thread2 : I'm thread 2\n");
  15.     sleep(3);
  16.     printf("thread2 : I'm thread 2 end\n");
  17.     exit(0);
  18. }
  19. void thread_create(void)
  20. {
  21. int temp;
  22. memset(&thread, 0, sizeof(thread));
  23. if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0)
  24. printf("create thread 1 fail\n");
  25. else
  26. printf("create thread 1 success\n");
  27. if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0)
  28. printf("create thread 2 fail\n");
  29. else
  30. printf("create thread 2 success\n");
  31. }
  32. void thread_wait(void)
  33. {
  34.     if(thread[0] !=0) { //comment4
  35. pthread_join(thread[0],NULL);
  36. printf("thread1 end\n");
  37. }
  38. if(thread[1] !=0) { //comment5
  39. pthread_join(thread[1],NULL);
  40. printf("thread2 end\n");
  41. }
  42. }
  43. int main()
  44. {
  45. printf("i am main\n");
  46. thread_create();
  47. printf("mail waitin \n");
  48. thread_wait();
  49. return 0;
  50. }
阅读(583) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~