代码一:
#include "../utili.h"
#define MAX_ITEM 10000
#define MAX_THREAD 5
void *produce(void *), *consume(void *);
struct {
pthread_mutex_t mutex;
int buff[MAX_ITEM];
int nput;
int nval;
} shared = {
PTHREAD_MUTEX_INITIALIZER
};
int main()
{
pthread_t tid_produce[MAX_ITEM], tid_consume;
int count[MAX_THREAD];
for(int i=0; i<MAX_THREAD; ++i){
count[i] = 0;
pthread_create(&tid_produce[i], NULL, produce, &count[i]);
}
for(int i=0; i<MAX_THREAD; ++i){
pthread_join(tid_produce[i], NULL);
printf("count[%d] = %d\n", i, count[i]);
}
pthread_create(&tid_consume, NULL, consume, NULL);
pthread_join(tid_consume, NULL);
return 0;
}
void *produce(void *arg)
{
for(; ;){
pthread_mutex_lock(&shared.mutex);
if(shared.nput >= MAX_ITEM){
pthread_mutex_unlock(&shared.mutex);
return (void *)0;
}
shared.buff[shared.nput] = shared.nval;
shared.nput++;
shared.nval++;
pthread_mutex_unlock(&shared.mutex);
*(int *)arg += 1;
}
}
void *consume(void *arg)
{
for(int i=0; i<MAX_ITEM; ++i){
if(shared.buff[i] != i)
printf("buff[%d] = %d\n", i, shared.buff[i]);
}
return (void *)0;
}
代码二:
#include "../utili.h"
#define MAX_ITEM 10000
#define MAX_THREAD 5
void *produce(void *), *consume(void *);
struct {
pthread_mutex_t mutex;
int buff[MAX_ITEM];
int nput;
int nval;
} shared = {
PTHREAD_MUTEX_INITIALIZER
};
int main()
{
pthread_t tid_produce[MAX_ITEM], tid_consume;
int count[MAX_THREAD];
pthread_setconcurrency(MAX_THREAD + 1);
for(int i=0; i<MAX_THREAD; ++i){
count[i] = 0;
pthread_create(&tid_produce[i], NULL, produce, &count[i]);
}
pthread_create(&tid_consume, NULL, consume, NULL);
for(int i=0; i<MAX_THREAD; ++i){
pthread_join(tid_produce[i], NULL);
printf("count[%d] = %d\n", i, count[i]);
}
pthread_join(tid_consume, NULL);
return 0;
}
void *produce(void *arg)
{
for(; ;){
pthread_mutex_lock(&shared.mutex);
if(shared.nput >= MAX_ITEM){
pthread_mutex_unlock(&shared.mutex);
return (void *)0;
}
shared.buff[shared.nput] = shared.nval;
shared.nput++;
shared.nval++;
pthread_mutex_unlock(&shared.mutex);
*(int *)arg += 1;
}
}
void consume_wait(int i)
{
for(; ;){
pthread_mutex_lock(&shared.mutex);
if(i < shared.nput){
pthread_mutex_unlock(&shared.mutex);
return ;
}
pthread_mutex_unlock(&shared.mutex);
}
}
void *consume(void *arg)
{
for(int i=0; i<MAX_ITEM; ++i){
consume_wait(i);
if(shared.buff[i] != i)
printf("buff[%d] = %d\n", i, shared.buff[i]);
}
return (void *)0;
}
代码三:
#include "../utili.h"
#define MAX_ITEM 10000
#define MAX_THREAD 5
void *produce(void *), *consume(void *);
int nitems;
int buff[MAX_ITEM];
struct {
pthread_mutex_t mutex;
int nput;
int nval;
} put = {
PTHREAD_MUTEX_INITIALIZER
};
struct {
pthread_mutex_t mutex;
pthread_cond_t cond;
int nready;
} nready = {
PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER
};
int main()
{
pthread_t tid_produce[MAX_ITEM], tid_consume;
int count[MAX_THREAD];
for(int i=0; i<MAX_THREAD; ++i){
count[i] = 0;
pthread_create(&tid_produce[i], NULL, produce, &count[i]);
}
for(int i=0; i<MAX_THREAD; ++i){
pthread_join(tid_produce[i], NULL);
printf("count[%d] = %d\n", i, count[i]);
}
pthread_create(&tid_consume, NULL, consume, NULL);
pthread_join(tid_consume, NULL);
return 0;
}
void *produce(void *arg)
{
for(; ;){
pthread_mutex_lock(&put.mutex);
if(put.nput >= MAX_ITEM){
pthread_mutex_unlock(&put.mutex);
return (void *)0;
}
buff[put.nput] = put.nval;
put.nput++;
put.nval++;
pthread_mutex_unlock(&put.mutex);
pthread_mutex_lock(&nready.mutex);
if(nready.nready == 0)
pthread_cond_signal(&nready.cond);
nready.nready++;
pthread_mutex_unlock(&nready.mutex);
*(int *)arg += 1;
}
}
void *consume(void *arg)
{
for(int i=0; i<MAX_ITEM; ++i){
pthread_mutex_lock(&nready.mutex);
while(nready.nready == 0)
pthread_cond_wait(&nready.cond, &nready.mutex);
nready.nready--;
pthread_mutex_unlock(&nready.mutex);
if(buff[i] != i)
printf("buff[%d] = %d\n", i, buff[i]);
}
return (void *)0;
}
头文件:
#pragma once
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#define min(x, y) ((x) < (y) ? (x) : (y))
#define err_exit(m) \
do{ \
perror(m); \
exit(1); \
}while(0)