linux c的sleep 和多线程测试

 一直担心在线程里加sleep会引起进程所有的线程挂起,测试过了不会,这样可以用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.  
  8. pthread_t thread[2]; 
  9. void * thread1() 
  10.     while(1)printf ("thread1 : I'm thread 1\n"); 
  11. void * thread2() 
  12. {  
  13.     printf("thread2 : I'm thread 2\n"); 
  14.     sleep(3); 
  15.     printf("thread2 : I'm thread 2 end\n"); 
  16.     exit(0); 
  17. void thread_create(void
  18. int temp; 
  19. memset(&thread, 0, sizeof(thread)); 
  20. if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0) 
  21. printf("create thread 1 fail\n"); 
  22. else 
  23. printf("create thread 1 success\n"); 
  24. if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0) 
  25. printf("create thread 2 fail\n"); 
  26. else 
  27. printf("create thread 2 success\n"); 
  28. void thread_wait(void
  29.     if(thread[0] !=0) { //comment4 
  30. pthread_join(thread[0],NULL); 
  31. printf("thread1 end\n"); 
  32. if(thread[1] !=0) { //comment5 
  33. pthread_join(thread[1],NULL); 
  34. printf("thread2 end\n"); 
  35. int main() 
  36. printf("i am main\n"); 
  37. thread_create(); 
  38. printf("mail waitin \n"); 
  39. thread_wait(); 
  40. return 0; 

      本文转自yifangyou 51CTO博客,原文链接:http://blog.51cto.com/yifangyou/603507,如需转载请自行联系原作者



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值