• 3个线程打印ABC


     

     

    1. #include <stdio.h>
    2. #include <pthread.h>
    3. #include <errno.h>
    4. #include <unistd.h>
    5. #include <stdlib.h>
    6. #include <string.h>
    7. pthread_cond_t cond1;
    8. pthread_cond_t cond2;
    9. pthread_cond_t cond3;
    10. pthread_mutex_t mutex;
    11. int flag=0; //0:A //1:B // 2:C
    12. void * A(void * arg)
    13. {
    14. int i=0;
    15. while(i<10)
    16. {
    17. pthread_mutex_lock(&mutex);
    18. if(flag!=0)
    19. {
    20. //其他线程运行时阻塞
    21. pthread_cond_wait(&cond1,&mutex);//当前线程睡在cond1上;
    22. //解开互斥锁,当前线程进入休眠状态等待别唤醒,时间片切换到另一个
    23. }
    24. printf("A");
    25. fflush(stdout);
    26. i++;
    27. sleep(1);
    28. flag=1;
    29. pthread_cond_signal(&cond2);
    30. //唤醒cond2下的线程
    31. pthread_mutex_unlock(&mutex);
    32. //解开互斥锁
    33. }
    34. pthread_exit(NULL);
    35. }
    36. void * B(void * arg)
    37. {
    38. int i=0;
    39. while(i<10)
    40. {
    41. pthread_mutex_lock(&mutex);
    42. if(flag!=1)
    43. {
    44. //其他线程运行时阻塞
    45. pthread_cond_wait(&cond2,&mutex);
    46. //解开互斥锁,当前线程进入休眠状态等待别唤醒,时间片切换到另一个
    47. }
    48. printf("B");
    49. fflush(stdout);
    50. i++;
    51. sleep(1);
    52. flag=2;
    53. pthread_cond_signal(&cond3);
    54. //唤醒cond3下的线程
    55. pthread_mutex_unlock(&mutex);
    56. //解开互斥锁
    57. }
    58. pthread_exit(NULL);
    59. }
    60. void * C(void * arg)
    61. {
    62. int i=0;
    63. while(i<10)
    64. {
    65. pthread_mutex_lock(&mutex);
    66. if(flag!=2)
    67. {
    68. //其他线程运行时阻塞
    69. pthread_cond_wait(&cond3,&mutex);
    70. //解开互斥锁,当前线程进入休眠状态等待别唤醒,时间片切换到另一个
    71. }
    72. printf("C");
    73. fflush(stdout);
    74. i++;
    75. sleep(1);
    76. flag=0;
    77. pthread_cond_signal(&cond1);
    78. //唤醒cond1下的线程
    79. pthread_mutex_unlock(&mutex);
    80. //解开互斥锁
    81. }
    82. pthread_exit(NULL);
    83. }
    84. int main(int argc, const char *argv[])
    85. {
    86. //申请互斥锁
    87. if(pthread_mutex_init(&mutex,NULL)!=0)
    88. {
    89. printf("申请失败");
    90. return -1;
    91. }
    92. pthread_t tid1;
    93. if(pthread_create(&tid1,NULL,A,NULL)!=0)
    94. {
    95. perror("pthread_create");
    96. return -1;
    97. }
    98. pthread_t tid2;
    99. if(pthread_create(&tid2,NULL,B,NULL)!=0)
    100. {
    101. perror("pthread_create");
    102. return -1;
    103. }
    104. pthread_t tid3;
    105. if(pthread_create(&tid3,NULL,C,NULL)!=0)
    106. {
    107. perror("pthread_create");
    108. return -1;
    109. }
    110. //初始化条件变量
    111. pthread_cond_init(&cond1,NULL);
    112. pthread_cond_init(&cond2,NULL);
    113. pthread_cond_init(&cond3,NULL);
    114. pthread_join(tid1,NULL);
    115. pthread_join(tid2,NULL);
    116. pthread_join(tid3,NULL);
    117. return 0;
    118. }

  • 相关阅读:
    Java学习任务总结【17】
    http: server gave HTTP response to HTTPS client 分析一下这个问题如何解决中文告诉我详细的解决方案
    2022.6.26 C++——使用面向对象的思想实现栈和日期
    V853开发板硬件框图及各模块原理图【内附PDF版本】
    JAVA中的垃圾回收器(1)
    SpringSecurity系列——RememberMe实战day7-3(源于官网5.7.2版本)
    基线提升至96.45%:2022 司法杯犯罪事实实体识别+数据蒸馏+主动学习
    短视频源码php
    网络代理技术:保障隐私与增强安全
    计算机组成原理(二)
  • 原文地址:https://blog.csdn.net/jinpaigangsi/article/details/127873867