Friday 15 April 2011

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


I have the following scenario: I have a thread that wants to fill a container with a pair of integers (summarize In, job description), and I have a large number of worker thread (8-16) that should take the element from this container and do some work.

I thought that the problem can be easily solved with a blocked queue - like removing items, synchronizing access to the thread queue, and sleeping if no data is available.

I (maybe wrong) I believe that something like that should be present in STL or Boost, but I am unable to find anything.

Do I really have to implement that thing myself? It seems that such a general scenario ...

If you apply it yourself, So implementation should be a fairly simple combination of a semaphore, muteux, and a line item.

Here's some pseudo-code:

  Generate {pthread_mutex_lock (& ​​amp; mutex); Queue.push_back (someObjectReference); Pthread_mutex_unlock (& ​​amp; amp; mutex); Sem_post (& amp; availabilitySem); } {Sem_wait (& amp; availability); Pthread_mutex_lock (& ​​amp; mutex); Queue.pop_front (someObjectReference); Pthread_mutext_unlock (& ​​amp; mutex); }  

No comments:

Post a Comment