void producer(char* line){
/* Produtor */
pthread_mutex_lock(&buffer_mutex);
while (occupied >= MAX_COMMANDS){
printf("produzi wait\n");
pthread_cond_wait(&less, &buffer_mutex);
}
printf("insere no index: %d\n",buff_write_idx);
insertCommand(line, buff_write_idx);
buff_write_idx = (buff_write_idx + 1) % MAX_COMMANDS;
occupied++;
printf("produzi. occupied = %d\n", occupied);
pthread_cond_signal(&more);
pthread_mutex_unlock(&buffer_mutex);
}
/******************************************************/
char* consumer(){
pthread_mutex_lock(&buffer_mutex);
while(occupied <= 0 ){
printf("consumidor wait\n");
pthread_cond_wait(&more, &buffer_mutex);
}
printf("retira do index: %d\n",buff_read_idx);
char* command = removeCommand(buff_read_idx);
buff_read_idx = (buff_read_idx + 1) % MAX_COMMANDS;
occupied--;
printf("consumidor. occupied = %d\n", occupied);
pthread_cond_signal(&less);
pthread_mutex_unlock(&buffer_mutex);
return command;
}
My problem is that after I run my program in the end, after the buffer done the operations necessary, in the CONSUMER(consumer while waiting for signal of producer) is always waiting for a producer but the producer is already empty. Suggestions to exit the loop?