int in, out, buffer[BUFFERSIZE]; mutex_t m; cond_var_t not_empty, not_full; while(more_to_produce){ mutex_lock(&m); if (out == (in + 1) % BUFFERSIZE) // buffer full condition_wait(¬_full); add_item(buffer[in]); in = (in + 1) % BUFFERSIZE cond_broadcast(¬_empty); } while (more_to_consume){ mutex_lock(&m); if(out == in) // buffer empty condtion_wait(¬_empty); remove_item(out); out = (out + 1) % BUFFERSIZE; condtion_signal(¬_empty); }