I have found pseudo code on how to implement a circular buffer.
// Producer.
while (true) {
/* produce item v */
while ((in+1)%n == out)
/* Wait. */;
b[in] = v;
in = (in + 1) % n
}
// Consumer.
while (true) {
while (in == out)
/* Wait. */;
w = b[out];
out = (out + 1) % n;
/* Consume item w. */
}
What I don't understand is the "Consume item w." comment, because I think that with w = b[out];
we are consuming w
, aren't we?
/* produce item v */
we need to assign v to a random number or something like that.. i thought we need a different process for ` /* Consume item w. */ ` – Heida