流水线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;}


原创粉丝点击