Building a multithreaded work-queue (consumer/producer) in C++

c++, deque, multithreading

Solution

If you do implement it yourself, the implementation should be a fairly straightforward combination of a semaphore, a mutex, and a queue object.

Here's some pseudo-code:

Produce{
    pthread_mutex_lock(&mutex);
    queue.push_back(someObjectReference);
    pthread_mutex_unlock(&mutex);
    sem_post(&availabilitySem);
}

Consume{
    sem_wait(&availabilitySem);
    pthread_mutex_lock(&mutex);
    queue.pop_front(someObjectReference);
    pthread_mutext_unlock(&mutex);
}

Problem

I have the following scenario: I have a single thread that is supposed to fill a container with pairs of integers (in essence, task descriptions), and I have a large number of worker threads (8-16) that should take elements from this container and perform some work. I thought the problem could be easily solved by a blocking queue -- e.g. on item-removal, threads synchronize access to the queue, and sleep if there is no data available. I (perhaps wrongly) assumed that something like this should exist in the STL or in boost, but I was unable to find anything. Do I actually have to implement that thing myself ? It seems like such a common scenario...

Original source