One problem I see is that your consumer doesn't actually check for work to do, it just blindly pops from the queue.
The second problem I see is that you increment count in one and decrement it in the other, so how do you ever reach the termination condition?
Take the ninja "count--" out of consumer and it should work. Still, you might want to do the following inside consumer:
// Wait for producer to do its thing and tell us there is work to do.
while ( myqueue.empty() ) {
pthread_cond_wait(&condition_var, &count_mutex);
}
// we've been told there's work to do with the queue,
// and we know there's something ON the queue.
// consume the entire queue.
while ( !myqueue.empty() ) {
myqueue.pop();
}
// treat count as protected by the mutex, so hoist this test into the lock.
bool workDone = (count >= COUNT_DONE);
pthread_mutex_unlock(&count_mutex);
if(workDone)
return break;
edit: preferred version of consumer:
bool workDone = false;
while(workDone == false)
{
// Lock mutex and then wait for signal to relase mutex
pthread_mutex_lock( &count_mutex );
// Wait for producer to do its thing and tell us there is work to do.
while ( myqueue.empty() )
pthread_cond_wait( &condition_var, &count_mutex );
// we've been told there's work to do with the queue,
// and we know there's something ON the queue.
// consume the entire queue.
while ( myqueue.empty() == false ) {
myqueue.pop();
}
// count is protected by the lock so check if we're done before we unlock.
workDone = (count >= COUNT_DONE);
pthread_mutex_unlock( &count_mutex );
}
return NULL;