I have written multi producer single consumer code with bounded buffer. Please review the code for any improvements or mistakes.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
#include <string.h>
#define SIZE 50
#define PRODUCERS 5
#define TOTAL 2
#define ENTER printf("\n-- %s -- %d --", __func__, __LINE__);
char (*buf_write)[20], (*buf_read)[20];
char act_buf[2][SIZE][20];
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_barrier_t barrier;
int write_index = 0, writer_cnt = 0;
void init();
/*
* Consumer
*/
void *consumer(void *vargp)
{
int i = 0, index = 0;
while (1) {
pthread_mutex_lock(&mutex);
if (writer_cnt != PRODUCERS)
pthread_cond_wait(&cond, &mutex);
pthread_mutex_unlock(&mutex);
writer_cnt = 0;
write_index = 0;
buf_read = &act_buf[index];
index = index + 1;
index = index % TOTAL;
buf_write = &act_buf[index];
pthread_barrier_wait(&barrier);
i = 0;
while (i < SIZE) {
printf("%s\t",buf_read[i]);
i++;
if (i%10 == 0)
printf("\n");
}
printf("\n");
memset(buf_read, 0, sizeof(act_buf) / 2);
}
}
/*
* Producer
*/
void *producer(void *vargp)
{
int i = 0;
char *str = (char *) vargp;
while (1) {
i = 0;
while (i < 10) {
pthread_mutex_lock(&lock);
str[0] = '0' + i;
strcpy(buf_write[write_index], str);
++write_index;
pthread_mutex_unlock(&lock);
++i;
}
pthread_mutex_lock(&mutex);
writer_cnt++;
if (writer_cnt == PRODUCERS)
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
pthread_barrier_wait(&barrier);
}
}
int main()
{
int i = 0;
pthread_t prouder_tid[PRODUCERS], consumer_tid;
char str[5][20] = {" -producer1"," -producer2"," -producer3"," -producer4"," -producer5"};
buf_write = &act_buf[0];
init();
for (i = 0; i < PRODUCERS; i++) {
pthread_create(&prouder_tid[i], NULL, producer, &str[i]);
}
pthread_create(&consumer_tid, NULL, consumer, NULL);
pthread_join(consumer_tid, NULL);
for (i = 0; i < PRODUCERS; i++) {
pthread_join(prouder_tid[i], NULL);
}
return 0;
}
void init()
{
//pthread_mutex_init(&lock, NULL);
//pthread_mutex_init(&writer_lock, NULL);
int s = 0;
s = pthread_barrier_init(&barrier, NULL, PRODUCERS + 1);
if (s != 0) {
printf("pthread_barrier_init failied");
exit(-1);
}
}