Originally Posted by
pheres
I guess I got something wrong regarding the concept of producer/consumer. how can I avoid the problem mentioned in the comment above?
Your use of the condition is not correct. More like:
Code:
lock mutex;
while(true)
{
wait for condition;
while(vector != empty)
{
pop element;
unlock mutex;
consume;
lock mutex;
}
}
unlock mutex;
You unlock/lock in the inner loop is not strictly necessary, but it prevents the producer from blocking while the consumer is processing. You could eliminate both problems by replacing that inner loop with:
Code:
if(vector != empty)
{
newvec = vector;
vector.clear();
unlock mutex;
consume_all(newvec);
lock mutex;
}
In other words, deque ALL the requests at once, and process them serially, without repeatedly locking/unlocking the original vector as you do so.