#include <stdio.h>
#include <pthread.h>
#define BUFFER_SIZE 4
#define OVER (-1)
struct producers //定義生產者條件變量結構。
{
int buffer[BUFFER_SIZE]; //定義緩沖區。
pthread_mutex_t lock; //定義訪問緩沖區的互斥鎖。
int readpos, writepos; //讀寫的位置。
pthread_cond_t notempty; //緩沖區有數據時的標記。
pthread_cond_t notfull; //緩沖區未滿的標記。
};
//初始化緩沖區
void init(struct producers *b)
{
pthread_mutex_init(&b->lock,NULL);
pthread_cond_init(&b->notempty,NULL);
pthread_cond_init(&b->notfull,NULL);
b->readpos=0;
b->writepos=0;
}
//在緩沖區中存放一個整數。
void put(struct producers *b, int data)
{
pthread_mutex_lock(&b->lock);
//當緩沖區為滿時等待。
while((b->writepos+1)%BUFFER_SIZE == b->readpos)
{
pthread_cond_wait(&b->notfull,&b->lock);
//在返回之前,pthread_cond_wait需要參數b->lock。
}
//向緩沖區中寫數據,并將寫指針向前移動。
b->buffer[b->writepos] = data;
b->writepos++;
if(b->writepos >= BUFFER_SIZE)
{
b->writepos=0;
}
//發送當前緩沖區中有數據的信號。
pthread_cond_signal(&b->notempty);
pthread_mutex_unlock(&b->lock);
}
//從緩沖區中讀數據并將數據從緩沖區中移走。
int get(struct producers *b)
{
int data;
pthread_mutex_lock(&b->lock);
//當緩沖區中有數據時等待。
while(b->writepos == b->readpos)
{
pthread_cond_wait(&b->notempty,&b->lock);
}
//從緩沖區中讀數據,并將指針前移。
data = b->buffer[b->readpos];
b->readpos++;
if(b->readpos >= BUFFER_SIZE)
{
b->readpos = 0;
}
//發送當前緩沖區未滿的信號。
pthread_cond_signal(&b->notfull);
pthread_mutex_unlock(&b->lock);
return data;
}
struct producers buffer;
//這是生產者的線程處理函數
void *producer(void *data)
{
int n;
for(n=0;n<10;n++)
{
printf("生產者: %d-->\n",n); //連續10次生產
put(&buffer,n);
}
put(&buffer,OVER); //將狀態放入buffer中
return NULL;
}
//這是消費者的線程處理函數
void *consumer(void *data)
{
int d;
while(1)
{
d = get(&buffer); //從buffer中讀取對應的狀態
if(d == OVER) //如果已經沒有了則停止
{
break;
}
printf("消費者: --> %d\n",d);
}
return NULL;
}
//這是主程序
int main(int argc,char *argv[])
{
pthread_t thproducer,thconsumer; //生產者和消費者的id
void *retval;
init(&buffer); //初始化緩沖區
pthread_create(&thproducer,NULL,producer,0);
pthread_create(&thconsumer,NULL,consumer,0); //創建兩個線程
pthread_join(thproducer,&retval);
pthread_join(thconsumer,&retval); //阻塞進程
return 0;
}免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。