Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1121229
  • 博文数量: 141
  • 博客积分: 2853
  • 博客等级: 少校
  • 技术积分: 2266
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-04 12:03
文章分类

全部博文(141)

文章存档

2014年(3)

2013年(12)

2012年(126)

分类: 嵌入式

2012-06-26 22:09:40

今天被坑了,以后要长记性了,int main(void),恶心的void,

点击(此处)折叠或打开

  1. /* thread.c */
  2. #include
  3. #include
  4. #include
  5. void *thrd_fun1(void *arg)
  6. {
  7. int no=(int)arg;
  8. int delay1;
  9. printf("Thread 1 is starting\n");
  10. while(1)
  11. {
  12. delay1=(int)(rand()*3.0/(RAND_MAX))+1;
  13. sleep(1);
  14. printf("thread 1 delay %d\n",delay1);
  15. }
  16. pthread_exit(NULL);
  17. }
  18. void *thrd_fun2(void *arg)
  19. {
  20. int no=(int)arg;
  21. int delay2;
  22. while(1)
  23. {
  24. delay2=(int)(rand()*10/(RAND_MAX))+1;
  25. printf("thread 2 delay %d\n",delay2);
  26. sleep(delay2);
  27. }
  28. pthread_exit(NULL);
  29. }
  30. int main(void)//最坑人的地方如果不加void会出现卡死现象
  31. {
  32. pthread_t thread[2];
  33. int res=0;
  34. int no=0;
  35. void *thrd_ret;
  36. srand(time(NULL));
  37. res=pthread_create(&thread[0],NULL,thrd_fun1,(void*)no);
  38. if(res!=0)
  39. {
  40. printf("Create thread 1 failed\n");
  41. exit(res);
  42. }
  43. res=pthread_create(&thread[1],NULL,thrd_fun2,(void*)no);
  44. if(res!=0)
  45. {
  46. printf("Create thread 2 failed\n");
  47. exit(res);
  48. }
  49. for(no=0;no<2;no++)
  50. {
  51. res=pthread_join(thread[no],&thrd_ret);
  52. if(!res)
  53. {
  54. printf("Thread %d exit\n",no);
  55. }
  56. else
  57. {
  58. printf("Thread %d exit failed",no);
  59. }
  60. }
  61. return 0;
  62. }

阅读(757) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~