#include <pthread.h>
#include <stdio.h>
const int MAX = 10;
const int loops = 100000;
int buffer[MAX];
int fill_ptr = 0;
int use_ptr = 0;
int count = 0;
void put(int value) {
buffer[fill_ptr] = value;
fill_ptr = (fill_ptr + 1) % MAX;
count++;
}
int get() {
int tmp = buffer[use_ptr];
use_ptr = (use_ptr + 1) % MAX;
count--;
return tmp;
}
pthread_cond_t empty, fill;
pthread_mutex_t mutex;
void* producer(void *arg);
void* producer(void *arg) {
int i;
for( i = 0; i < loops; i++ ){
pthread_mutex_lock(&mutex);
while (count == MAX){
pthread_cond_wait(&empty, &mutex);
}
put(i);
printf("Produced an item (count: %d)\n", count);
pthread_cond_signal(&fill);
pthread_mutex_unlock(&mutex);
}
return NULL;
}
void* consumer(void *arg) {
int i;
for (i = 0; i < loops/2; i++){
pthread_mutex_lock(&mutex);
while (count == 0){
pthread_cond_wait(&fill, &mutex);
}
int tmp = get();
printf("Consumed an item (count: %d)\n", count);
pthread_cond_signal(&empty);
pthread_mutex_unlock(&mutex);
printf("%d\n", tmp);
}
return NULL;
}
int main(int argc, char *argv[]) {
pthread_t p, c1, c2;
pthread_create( &p, NULL, producer, NULL );
pthread_create( &c1, NULL, consumer, NULL );
pthread_create( &c2, NULL, consumer, NULL );
pthread_join(p, NULL);
pthread_join(c1, NULL);
pthread_join(c2, NULL);
return 0;
}