线程的组织方式:流水线结构

本文深入探讨线程的组织方式,重点聚焦于流水线结构。通过流水线,可以实现任务的高效并行处理,提升系统性能。文章将详细解析流水线的工作原理及其在多线程环境中的应用。
摘要由CSDN通过智能技术生成

流水线结构

#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 &#
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值