流水线结构
#include <pthread.h>
#include "errors.h"
typedef struct stage_tag {
pthread_mutex_t mutex; // 保持本阶段互斥的互斥锁
pthread_cond_t avail; // 是否空闲
pthread_cond_t ready; // 是否有数据
int data_ready; // 有数据标记
long data;
pthread_t thread; // 相应的线程
struct stage_tag *next;
} stage_t;
typedef struct pipe_tag {
pthread_mutex_t mutex;
stage_t *head, *tail;
int stages;
int active;
} pipe_t;
int pipe_send( stage_t *stage, long data )
{
int status;
status = pthread_mutex_lock( &stage -> mutex );
if( status != 0 )
return status;
while( stage -> data_ready ) { // 如果有数据,则下面必须等待知道收到ready信号
status = pthread_cond_wait( &stage -> ready, &stage -> mutex );
if( status != 0 ) {
pthread_mutex_unlock( &stage -> mutex );
return status;
}
}
// 空闲则可以继续执行,传递data,置就绪位
stage -> data = data;
stage -> data_ready &#