码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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. }

  • 相关阅读:
    怎么开启22端口访问权限,让别的机器通过ssh或者向日葵等远程控制工具链接
    LQ0274 密码发生器【水题】
    计算机网络TCP篇①
    如何进行远程adb真机调试?
    CentOS-7下安装及配置vsftpd详细步骤(可匿名访问)
    从零开始构建并编写神经网络---Keras【学习笔记】[1/2]
    socket开发步骤及相关API介绍
    GLOG 日志宏分析与PR合并
    考研资讯平台|基于springboot框架+ Mysql+Java+Tomcat的考研资讯平台设计与实现(可运行源码+数据库+设计文档)
    十八、openlayers官网示例Custom Animation解析——地图上添加自定义动画
  • 原文地址:https://blog.csdn.net/jinpaigangsi/article/details/127873867
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号