producer / consumer question
Hi,
my consumer waits for a producer putting things in a vector as in the pseudo code below:
Code:
while(true)
{
lock mutex;
wait_for_condition;
// ok, the condition was signaled by the producer
unlock mutex;
// the producer can now put new things in
// and they are going to be proper handled because of the while-loop
while(vector != empty)
{
lock mutex;
pop element;
unlock mutex;
consume;
}
// now this place is interesting:
// the while loop is ended so all things are consumed
// but exactly at this point a new thing is produced and put in the vector
// and the condition is signaled
// but the consumer does not wait yet
// so I guess the consumer is starving until a second thing is produced
}
I guess I got something wrong regarding the concept of producer/consumer. how can I avoid the problem mentioned in the comment above?
Thank you in advance!