单生产者 单消费者
来源:互联网 发布:人脸识别算法 开源 编辑:程序博客网 时间:2024/03/29 23:02
#include <stdio.h>#include <pthread.h>struct event_loop_buf{int head,tail;//@range 0~49 int eventnum[50];};volatile struct event_loop_buf evlop[6];init_evlop(){int i,j;for(i=0; i<6; i++){evlop[i].head = 0;evlop[i].tail = 0;for(j=0;j<50;j++){evlop[i].eventnum[j] = -1;}}}int get_a_event(int line){int ret = 0;//head is the first valid data int head_next = evlop[line].head+1 > 49 ? 0 : evlop[line].head+1;if(evlop[line].head != evlop[line].tail){if(evlop[line].eventnum[evlop[line].head] != -1){ret = evlop[line].eventnum[evlop[line].head];evlop[line].eventnum[evlop[line].head] = -1;}else{ret = -1;}evlop[line].head = head_next;}else{if(evlop[line].eventnum[evlop[line].head] != -1){//read head;//clear head;ret = evlop[line].eventnum[evlop[line].head];evlop[line].eventnum[evlop[line].head] = -1;}else{ret = -1;}}return ret;}int insert_a_event(int line,int in){//tail is last valid data int tail_next = evlop[line].tail+1 > 49 ? 0 : evlop[line].tail+1;if(tail_next != evlop[line].head){evlop[line].eventnum[tail_next] = in;evlop[line].tail = tail_next;}else{return -1;}return 1;}void getev(){int ret,count=0;while(1){while(-1 == (ret = get_a_event(1)));printf(" get %d ret is %d\n",++count,ret);//sleep(1);}}int main(){int n,count=0,ret;pthread_create(&n,NULL,getev,NULL);init_evlop(); int i = 400;while(i-- > 0){while(-1 == (ret = insert_a_event(1,22+count)));printf(" %d insertret %d is %d\n",++count,22+count,ret);//sleep(3);} while(1)sleep(5);}
0 0
- 单生产者,单消费者
- 单生产者 单消费者
- 单生产者-单消费者模型
- 单生产者 多消费者
- 单生产者-多消费者/CountDownLatch
- 单缓冲区--生产者-消费者问题
- C++11-->单生产者,单消费者问题
- java多生产者,单消费者实现
- 生产者 消费者 --- 多线程 到 单线程 控制
- C++ 单生产者多消费者多线程示例
- 单生产者单消费者循环无锁队列
- 单生产者 单消费者情景下 无锁仓库
- 单生产者,单消费者无锁队列实现(c)
- Java线程之单生产者单消费者示例(二)
- 笔试题:单消费者和单生产者的问题
- 单生产者单消费者模式的无锁队列实现
- 无锁队列之单生产者单消费者
- 基于环形队列的单生产者单消费者模型
- Json数据格式介绍、使用Android自带类库完成Json生成与解析
- Handler消息传递机制
- java中的static用法
- Sonar初识
- Java 过滤器读取配置文件实现过滤编码
- 单生产者 单消费者
- UESTC 1170 红与蓝 计算几何、贪心、红蓝点对
- maven基础
- 十五、解释器模式Interpreter(行为型)
- 2440: [中山市选2011]完全平方数
- Thinkphp3.2+PHPQRCode 二维码生成示例
- 原型继承例子,看各自的prototype和constructor
- Docker镜像与仓库
- 简单易懂的背包问题