有没有接触过C++的朋友看到上篇文章,希望给出C程序的实现。 匆忙之中没有以通用的的函数形式给出,而是将缓冲区代码与测试代码混合一个源程序,程序也写得丑陋了一些,仅供朋友们学习实现的原理。程序在linux 环境下已测试通过。 # include <limits.h> # include <pthread.h> # include <stdlib.h> # include <stdio.h> # define MAXNTHREADS 100 # define NBUFF 20 typedef struct { pthread_cond_t cond; int resource; } Cond; typedef struct { pthread_t id; int seq; } Data; Cond getCond = { PTHREAD_COND_INITIALIZER, 0 }; Cond putCond = { PTHREAD_COND_INITIALIZER, NBUFF }; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; Data buff[NBUFF]; int front = 0; int rear = 0; int size = 0; int run = 1; void stop() { run = 0; pthread_mutex_lock(&mutex); getCond.resource = INT_MAX / 2; pthread_cond_broadcast(&getCond.cond)