title: How to distribute tasks properly across multiple threads
body: <p>I have a computationally intensive program that could benefit from a simple parallelization since each task in a list of tasks is independent of one another.</p>
  
  <p>I'm not all that familiar with the theory that underlies computing in parallel and I can't seem to find a clear answer to my question which is:</p>
  
  <p><strong>How do I distribute m tasks across n threads in a clean, canonical way?</strong></p>
  
  <p>My solution to this would be to run "blocks" of processes to be handled, i.e. allocate the first n tasks in the list to the n threads available, then (and here I think it's fair to make the assumption that in my particular program all tasks will take roughly the same time) after that block has been completed, allocate the next n tasks and so on, and if the number of tasks is not divisible by n, take the remaining k &lt; n tasks and execute them on k threads while the remaining n-k threads idle.</p>
  
  <p>In C++ I'm sure I could accomplish this fairly easily with a for loop and the % operator. I know it's not the most efficient way to do it but I think the speedup from doing it "properly" (i.e. ensuring no threads idle) is negligible in my particular case.</p>
  
  <p>Is this the right way to do it? Or is it perhaps possible, if I have a total m tasks to allocate m threads to the job and let the processor deal with distributing the tasks?</p>