#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#define BUFFER_SIZE 8
struct Products
{
int buffer[BUFFER_SIZE];
/*保證存取操作的原子性 互斥性*/
pthread_mutex_t locker;
/*是否可讀*/
pthread_cond_t notEmpty;
/*是否可寫(xiě)*/
pthread_cond_t notFull;
int posReadFrom;
int posWriteTo;
};
int BufferIsFull(struct Products* products)
{
if ((products->posWriteTo + 1) % BUFFER_SIZE == products->posReadFrom)
{
return (1);
}
return (0);
}
int BufferIsEmpty(struct Products* products)
{
if (products->posWriteTo == products->posReadFrom)
{
return (1);
}
return (0);
}
/*制造產(chǎn)品*/。
void Produce(struct Products* products, int item)
{
/*原子操作*/
pthread_mutex_lock(&products->locker);
/*無(wú)空間可寫(xiě)入*/
while (BufferIsFull(products))
{
pthread_cond_wait(&products->notFull, &products->locker);
}
/*寫(xiě)入數(shù)據(jù)*/
products->buffer[products->posWriteTo] = item;
products->posWriteTo++;
if (products->posWriteTo >= BUFFER_SIZE)
products->posWriteTo = 0;
/*發(fā)信*/
pthread_cond_signal(&products->notEmpty);
/*解鎖*/
pthread_mutex_unlock(&products->locker);
}
int Consume(struct Products* products)
{
int item;
pthread_mutex_lock(&products->locker);
/*為空時(shí)持續(xù)等待,無(wú)數(shù)據(jù)可讀*/
while (BufferIsEmpty(products))
{
pthread_cond_wait(&products->notEmpty, &products->locker);
}
/*提取數(shù)據(jù)*/
item = products->buffer[products->posReadFrom];
products->posReadFrom++;
/*如果到末尾,從頭讀取*/
if (products->posReadFrom >= BUFFER_SIZE)
products->posReadFrom = 0;
pthread_cond_signal(&products->notFull);
pthread_mutex_unlock(&products->locker);
return item;
}
#define END_FLAG (-1)
struct Products products;
void* ProducerThread(void* data)
{
int i;
for (i = 0; i < 16; ++i)
{
printf("producer: %d\n", i);
Produce(&products, i);
}
Produce(&products, END_FLAG);
return NULL;
}
void* ConsumerThread(void* data)
{
int item;
while (1)
{
item = Consume(&products);
if (END_FLAG == item)
break;
printf("consumer: %d\n", item);
}
return (NULL);
}
int main(int argc, char* argv[])
{
pthread_t producer;
pthread_t consumer;
int result;
pthread_create(&producer, NULL, &ProducerThread, NULL);
pthread_create(&consumer, NULL, &ConsumerThread, NULL);
pthread_join(producer, (void *)&result);
pthread_join(consumer, (void *)&result);
exit(EXIT_SUCCESS);
}