流水线pipeline
来源:互联网 发布:klairs淘宝官网 编辑:程序博客网 时间:2024/05/01 10:20
设计的思想为: 来自posix多线程编程
输入 | 线程1 |
线程2 | 线程3 | 输出设计思维提供给大家:
1、pipe_create创建一个流水线,流水线的工作下的线程数量为设置,
2、pipe_send 发送给下一个工作元素;
3、ready 、avail 前一个表示为该准备好了数据,通知stage_ready,avail:作为的是通知下一个工作做好准备。
4、创建的pipe_t 结构体,创建的线程数量,活跃的数量,pipe_reasult取出其中资源;
5、其实自己下面的代码在这里没有写注释很不好;
6、我认为是要学的设计思路,不是怎样去写出代码,而是明白编程的核心部分,不是在这里要做什么工程问题。希望大家多多指导
typedef struct stage_tag{ pthread_mutex_t mutex; pthread_cond_t ready; pthread_cond_t avail; pthread_t tid; long data; int stage_ready; struct stage_tag * next;}stage_t;typedef struct pipe_tag{pthread_mutex_t mutex;int active;int steps;stage_t *head;stage_t *tail;}pipe_t;int pipe_send(stage_t * stage,long data){ int status; status=pthread_mutex_lock(&stage->mutex); if(status!=0) err_abort(status, "send lock error"); while(stage->stage_ready) { status=pthread_cond_wait(&stage->ready, &stage->mutex); if(status!=0) { Pthread_mutex_lock(&stage->mutex); return status; } } stage->data=data; stage->stage_ready=0; status=pthread_cond_signal(&stage->avail); if(status!=0) { pthread_mutex_unlock(&stage->mutex); return status; } status=pthread_mutex_unlock(&stage->mutex); return status;}void * pipe_stage(void * arg){ stage_t * stage=(stage_t *)arg; stage_t * stage_next=stage->next; int status; Pthread_mutex_lock(&stage->mutex); while (1) { while(stage->stage_ready) { status=pthread_cond_wait(&stage->avail,&stage->mutex ); if(status!=0) { err_abort(status, "error in pipe"); } } pipe_send(stage_next, stage->data+1); stage->stage_ready=0; status=pthread_cond_signal(&stage->ready); if(status!=0) err_abort(status, "error in send"); /* code */ }}int pipe_create(pipe_t * pip,int steps){ stage_t **link,*newstage,*stage; int status,index; link=&pip->head; pthread_mutex_init(&pip->mutex,NULL); status=pthread_mutex_lock(&pip->mutex); if(status!=0) { err_abort(status,"error in pthread in pip"); } pip->active=0; pip->steps=steps; for(index=0;indexmutex, NULL); if(status!=0) err_abort(status, "error"); status=pthread_cond_init(&stage->ready,NULL); if(status!=0) err_abort(status, "error"); status=pthread_cond_init(&stage->avail,NULL); if(status!=0) err_abort(status, "error"); *link=stage; link=&stage->next; } *link=NULL; pip->tail=stage; for(newstage=pip->head;newstage!=NULL;newstage=newstage->next) { Pthread_create(&newstage->tid,NULL,pipe_stage,newstage); } return 0;}int pipe_start(pipe_t *pip,long value){ int status; status=pthread_mutex_lock(&pip->mutex); if(status!=0) err_abort(status, "error in lock"); pip->active++; status=pthread_mutex_unlock(&pip->mutex); if(status!=0) err_abort(status, "error"); pipe_send(pip->head,value); return 0;}int pipe_reasult(pipe_t *pip,stage_t * result){ stage_t * tail=pip->tail; int empty=0; Pthread_mutex_lock(&pip->mutex); if(pip->active<=0) empty=1; else pip->active--; if(empty) return 0; Pthread_mutex_unlock(&pip->mutex); while(tail->stage_ready) pthread_cond_wait(&tail->avail,&tail->mutex); *result=*tail; tail->stage_ready=0; pthread_cond_signal(&tail->ready); Pthread_mutex_unlock(&tail->mutex); return 1;}
阅读全文
0 0
- 流水线pipeline
- ARM流水线(pipeline)
- Pipeline学习器流水线
- 3D流水线(pipeline)
- sklearn 流水线(pipeline)使用
- Jenkins + Pipeline 构建流水线发布
- Intel TBB:Pipeline,软件流水线的威力
- Python中学习器流水线Pipeline
- 提高设计频率之基本功:流水线设计(pipeline Design)
- 多线程设计模式——Pipeline(流水线)模式
- Verilog十大基本功1(流水线设计Pipeline Design)
- java多线程设计模式 -- 流水线模式(Pipeline)
- pytorch学习笔记(四):输入流水线(input pipeline)
- pytorch学习笔记(四):输入流水线(input pipeline)
- Verilog十大基本功1(流水线设计Pipeline Design)
- 习题7-5 流水线调度(Pipeline Scheduling, UVa690)
- 流水线
- 流水线
- Git与Github建立密钥
- java 使用jdbc连接Greenplum数据库
- 正则表达式入门
- chapter21 Java多线程编程在Netty中的应用
- Python 流程控制
- 流水线pipeline
- Oracle之三大连接的使用限制,nl、hash、merge
- Android Studio jar、so、library项目依赖
- java求解蚂蚁爬杆问题(面向对象 暴力解法)
- android audio 音量设置分析
- chapter22 高性能之道
- 设计模式之单例模式
- Spring boot oauth2 研究第N天 action 2
- Huffman Codes(30 分)