linux两个线程交替打印数字
来源:互联网 发布:汉王小龙女手写板软件 编辑:程序博客网 时间:2024/05/18 06:16
#include<stdio.h>#include<unistd.h>#include<stdlib.h>#include<pthread.h>#define COUNT 10static int i = 1;pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;pthread_cond_t even = PTHREAD_COND_INITIALIZER;pthread_cond_t uneven = PTHREAD_COND_INITIALIZER;//1 3 5 7 9void* funA(void *arg){ while(i <= COUNT) { pthread_mutex_lock(&mutex); if(i % 2 != 0) { printf("funA : %d\n",i); ++i; pthread_cond_signal(&uneven); } else pthread_cond_wait(&even, &mutex); pthread_mutex_unlock(&mutex); }}//2 4 6 8 10void* funA(void *arg){ while(i <= COUNT) { pthread_mutex_lock(&mutex); if(i % 2 == 0) { printf("funB : %d\n",i); ++i; pthread_cond_signal(&even); } else pthread_cond_wait(&uneven, &mutex); pthread_mutex_unlock(&mutex); }}int main(){ pthread_t tid1, tid2; pthread_create(&tid1, NULL, A_fun, NULL); pthread_create(&tid2, NULL, B_fun, NULL); pthread_join(tid1, NULL); pthread_join(tid2, NULL); return 0;}
阅读全文
0 0
- linux两个线程交替打印数字
- 面试题一:实现两个线程交替打印数字
- 两个线程交替打印ABABAB
- 两个线程交替打印ABABAB
- 两个线程交替打印ABCDEF
- 两个线程的交替打印
- 两线程交替打印数字
- 两个线程交替打印字母 java
- 08 两个线程交替打印121212...
- Java实现两个线程交替打印问题
- wait,notify实现两个线程交替打印
- 多线程交替打印数字
- 多线程交替打印数字
- 两个线程每隔一秒钟交替打印5个数
- Java两个线程交替打印(实现Runnable接口)
- 多线程技术: 两个线程交替打印奇数和偶数
- 多线程同步之事件 -- 2个线程交替打印数字
- 2.使用线程实现数字与代码的交替打印
- nvm简介
- 数论的一些证明
- 使用windows10下搭建flask开发环境(python 3)
- 读书随笔1
- EOJ月赛总结
- linux两个线程交替打印数字
- unity 按钮绑定事件(NGUI UGUI)
- 常用命令
- javaScript 小结
- form表单验证,提交数据显示
- RecycleView的使用
- 自己动手实现mybatis动态sql
- SLAM方法汇总
- 为什么接口中的属性必须用public static final修饰?