栈实现模型:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
*// 仓库容量*
#define STORE_SIZE (20)
*// 仓库*
char store[STORE_SIZE];
*// 数量*
size_t store_cnt = 0;
pthread_mutex_t lock;
pthread_cond_t cond_empty;
pthread_cond_t cond_full;
void show_store(const char* who,const char* act,char data)
{
printf("[");
for(int i=0; i<store_cnt; i++)
{
printf("%c",store[i]);
}
printf("]%s%c : %s\n",act,data,who);
}
void* prun(void* arg)
{
for( ; ; )
{
*// 加锁*
pthread_mutex_lock(&lock);
while(store_cnt >= STORE_SIZE)
{
*// 此时仓库已满,生产者睡入仓库满的条件变量*
pthread_cond_wait(&cond_full,&lock);
}
*// 生产的数据*
char data = rand() % 26 + 'A';
show_store("生产者","<-",data);
store[store_cnt++] = data;
usleep(rand()%30000);
*// 解锁*
pthread_mutex_unlock(&lock);
// 叫醒所有睡入仓库空条件变量的线程
// pthread_cond_signal(&cond_empty);
pthread_cond_broadcast(&cond_empty);
}
}
void* crun(void* arg)
{
for( ; ; )
{
*// 加锁*
pthread_mutex_lock(&lock);
while(0 >= store_cnt)
{
// 此时仓库为空,生产者睡入仓库空的条件变量
pthread_cond_wait(&cond_empty,&lock);
}
*// 消费的数据*
char data = store[--store_cnt];
show_store("消费者","->",data);
usleep(rand()%30000);
*// 解锁*
pthread_mutex_unlock(&lock);
*// 叫醒所有睡入仓库满条件变量的线程*
// pthread_cond_signal(&cond_full);
pthread_cond_broadcast(&cond_full);
}
}
int main()
{
srand(time(NULL));
*// 初始化互斥量与条件变量*
pthread_mutex_init(&lock,NULL);
pthread_cond_init(&cond_empty,NULL);
pthread_cond_init(&cond_full,NULL);
pthread_t tid[10];
*//创建生产者线程*
for(int i=0; i<5; i++)
{
if(i%2)
pthread_create(&tid[i],NULL,prun,NULL);
else
pthread_create(&tid[i],NULL,crun,NULL);
}
*// 等待所有线程结束*
for(int i=0; i<10; i++)
{
pthread_join(tid[i],NULL);
}
}
队列模型实现:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
*// 仓库容量*
#define STORE_SIZE (20)
*// 仓库*
char store[STORE_SIZE];
*// 队头和队尾*
int front = 0 , rear = 0;
pthread_mutex_t flock;
pthread_mutex_t rlock;
pthread_cond_t cond_empty;
pthread_cond_t cond_full;
void show_store(const char* who,const char* act,char data)
{
printf("[");
for(int i=front; i!=rear; i=(i+1)%STORE_SIZE)
{
printf("%c",store[i]);
}
printf("]%s%c : %s\n",act,data,who);
}
void* prun(void* arg)
{
for(;;)
{
*// 加锁*
pthread_mutex_lock(&flock);
while((rear+1)%STORE_SIZE == front)
{
// 此时仓库已满,生产者睡入仓库满的条件变量
pthread_cond_wait(&cond_full,&flock);
}
*// 生产的数据*
char data = rand() % 26 + 'A';
show_store("生产者","<-",data);
store[rear++] = data;
rear %= STORE_SIZE;
usleep(rand()%500000);
*// 叫醒所有睡入仓库空条件变量的线程*
pthread_cond_broadcast(&cond_empty);
*// 解锁*
pthread_mutex_unlock(&flock);
}
}
void* crun(void* arg)
{
for(;;)
{
*// 加锁*
pthread_mutex_lock(&rlock);
while(front == rear)
{
*// 此时仓库为空,生产者睡入仓库空的条件变量*
pthread_cond_wait(&cond_empty,&rlock);
}
*// 消费的数据*
char data = store[front++];
front %= STORE_SIZE;
show_store("消费者","->",data);
usleep(rand()%500000);
*// 叫醒所有睡入仓库满条件变量的线程*
pthread_cond_broadcast(&cond_full);
*// 解锁*
pthread_mutex_unlock(&rlock);
}
}
int main()
{
srand(time(NULL));
*// 初始化互斥量与条件变量*
pthread_mutex_init(&flock,NULL);
pthread_mutex_init(&rlock,NULL);
pthread_cond_init(&cond_empty,NULL);
pthread_cond_init(&cond_full,NULL);
pthread_t tid[10];
*//创建生产者线程*
for(int i=0; i<10; i++)
{
if(i%2)
pthread_create(&tid[i],NULL,prun,NULL);
else
pthread_create(&tid[i],NULL,crun,NULL);
}
*// 等待所有线程结束*
for(int i=0; i<10; i++)
{
pthread_join(tid[i],NULL);
}
}