title: C++, How to implement Thread pool for tasks when each worker thread has to do few different tasks
body: <p>I am analyzing a video stream. For each new image (frame) i do the following 3 tasks sequentially:</p>
  
  <ol>
  <li>Reduce the size of the image</li>
  <li>Detect faces</li>
  <li>Track the 4 most important faces in the image</li>
  </ol>
  
  <p>In order to speed this up on a 4-cpu machine I use 4 worker threads. In the following way</p>
  
  <ol>
  <li>Main process gets an image. Creates 4 worker threads, split the image to 4 quarters, each worker re-sizes its 1/4 of the image pixels. Main process waits for the threads to finish and assembles the quarters to final image</li>
  <li>Main process creates 4 new workers for face detection. I detect 4 types of faces (male, female, baby, dog). Each worker thread is responsible for one type. Main process waits for the workers to finish and assembles the results (list of all the existing faces).</li>
  <li>Main process creates 4 new workers for face tracking. 4 most important faces are selected and each worker tracks 1 face. Main process waits for completion.</li>
  </ol>
  
  <p>The problem with my implementation is that I don't have a thread pool. On each video frame (roughly 30 times per second) the main process rises and kills 12 workers (4 workers x 3 different tasks). So a lot of time is wasted on thread management. Currently I use <code>_beginthreadex()</code> method to lunch a worker thread for a specific task</p>
  
  <p><strong>Desired solution:</strong> I want to create only once 4 worker threads (each worker is able to perform all 3 different tasks). Those workers will exists through out the entire video processing. On each video frame the main process will throw the image re-size task to the workers, than the detection and later the tracking.</p>
  
  <p><strong>An ugly implementation</strong> would be that each worker thread is a big function with implementation of all the 3 tasks. Main process just tells each worker which task to execute (worker has a 'switch' statement to select the requested task). This is an ugly solution because in the future when I will have 30 different tasks in the pipeline instead of 3 - the code of the workers will become enormous. Moreover this solution violates encapsulation rule because it requires all the tasks to reside in the same function + for each new task, I need to change the code of the worker</p>
  
  <p><strong>A clean implementation</strong> would be that main process gives each worker a pointer to a function (which task to execute) and some parameters. Thus I can easily add new tasks in my video processing pipeline without changing the code of the worker because the code of the worker is generic (execute a pointer to a function and wait until a request with new pointer arrives)
  But the problem here is that each task has a different amount of parameters (different function interface) and worker does not know how to call/execute the address of a given function.</p>
  
  <p>What is a good way to use a thread pool in my case, while keeping code generic as possible, and able to extend it from 3 tasks to 30.</p>
  
  <p>P.s. - My code runs on any platform (Android, iOS, linux, windows server, windows phone, etc). So I prefer a generic solution, instead of an OS specific or compiler specific solution</p>