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

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


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);}


If you are on windows take a look at the agents library in VS2010 this is a core scenario.

http://msdn.microsoft.com/en-us/library/dd492627(VS.100).aspx

i.e.

//an unbounded_buffer is like a queueunbounded_buffer<int> buf;//you can send messages into it with send or asendsend(buf,1);//receive will block and wait for dataint result = receive(buf)

you can use threads, 'agents' or 'tasks' to get the data out... or you can link buffers together and convert your blocking semantic producer / consumer problem to a data flow network.


If you are on Windows and want a queue that is efficient in terms of how it manages the threads that are allowed to run to process items from it then take a look at IO Completion Ports (see here). My free server framework includes a task queue implementation that's based on IOCPs and that may also be of interest if you intend to go down this route; though it's possibly too specialised for what you want.