I read a lecture about synchronization.
In this lecture there were slides about the bounded buffer problem (see below).
My question is: Why it's true that producer can only fill up to n-1 buffer cells? thanks!
I read a lecture about synchronization.
In this lecture there were slides about the bounded buffer problem (see below).
My question is: Why it's true that producer can only fill up to n-1 buffer cells? thanks!
It's not true that the producer can only fill up $n-1$ elements here. I understand that this did hold in your last question, but here it doesn't. Specifically, you initialize $empty$ to $n$, and thus the producer can also go $n$ times through its loop and thus fill the complete buffer (to see this, just assume that the consumer thread is not started)
The main differences to your previous example are: (i) you have more variables. Specifically, additionally to the buffer positions the consumer wants to read next and the producer wants to write next, you now directly store how many buffer positions are still free (variable $empty$) and occupied ($full=n-empty$). Thus, even when the two buffer locations are the same, you always know if the buffer is completely empty or full; and (ii) you use semaphores and muteces, i.e. structures specifically made for multi-threading which allow you to safely do certain operations which could otherwise lead to race conditions.