title: Threadpool / Queueing system in C++
body: <p>I have a situation in which I need to do some heavy computation. I found out that subdividing my data and then merging it back together is the fastest (as the size increases, time increases faster, so splitting is logical).</p>
  
  <p>It should be able to give a data size to the application, let's say for example one million double values.</p>
  
  <p>What I have in place now, is sending created data based on this size off to some function, returning it after computation, and then looping over the return to unload this data into the main vector.</p>
  
  <p>I want to send parts of 200, with one "last" part. For example, giving size = 1000005 will perform this function 5000 times initially, and then the last one with data of size 5.</p>
  
  <pre><code>int size = 1000000;
  int times = size / 200; // 5000
  int leftover = size % 200; // 0, this not performed
  
  QVector&lt;double&gt; x(size);
  QVector&lt;double&gt; y(size);
  
  x = createData(size);
  x = createData(size);
  
  for (int i = 0; i &lt; times; i++)
  {
      holder = createData(200);
      QVector&lt;double&gt; tempx = x.mid(i*200, 200);
      QVector&lt;double&gt; tempy = y.mid(i*200, 200);
      holder = myfunction(tempx, tempy, 200);  // let it now just return `tempy`
      for (int j = 0; j &lt; 200; j++)
      {
          y[i*200 + j] = holder[j];
      }
  }
  // leftover function here, really similar to this part before.
  
  // plotting function here
  </code></pre>
  
  <p>At the end, <code>x</code> will remain as initialized, <code>y</code> will have had the computation upon.</p>
  
  <p>Since these code parts can run apart from each other and speed is crucial, I would like to make use of several cores.</p>
  
  <p>The following further characterizes the situation:</p>
  
  <ul>
  <li>These function calls are independent of each other, only in the end when the vectors are complete do I want to plot the result. </li>
  <li>The time of completion for each call will be varying a lot.</li>
  <li>The amount of <code>times</code> should be variable.</li>
  </ul>
  
  <p>I read something about that maximum threads are advised to be the amount of cores (at least as a starting point), since using too many threads could slow the process down. Considering the situation a queueing system / threadpool would seem to make sense as not to lose time while one thread has some easy jobs and the others are slowing everything down by harder jobs.</p>
  
  <p>While it seems easy to print some messages using some (usually 2) threads in some dozen of tutorials, could anyone provide more detailed help on how to return vectors and unload these thread safely into a main function, and how to create a threadpool so time would not be wasted?</p>
  
  <p>Using Ubuntu 13.04, Qt and C++11x, though it should not matter.</p>