title: Dividing processes evenly among threads
body: <p>I am trying to come up with an algorithm to divide a number of processes as evenly as possible over a number of threads.  Each process takes the same amount of time.
  The number of processes can vary, from 1 to 1 million.  The threadCount is fixed, and can be anywhere from 4 to 48.</p>
  
  <p>The code below does divide all the work evenly, except for the last case, where I throw in what is left over.
      Is there a way to fix this so that the work is spread more evenly?</p>
  
  <pre><code>    void main(void)
  {
      int processBegin[100];
      int processEnd[100];
      int activeProcessCount = 6243;
      int threadCount = 24;
  
  int processsInBundle  = (int) (activeProcessCount / threadCount);
  int processBalance    = activeProcessCount - (processsInBundle * threadCount);
  
  for (int i = 0; i &lt; threadCount; ++i)
  {
      processBegin[ i ] = i * processsInBundle;
      processEnd[ i ]   = (processBegin[ i ] +  processsInBundle) - 1;
  }
  
  processEnd[ threadCount - 1 ] += processBalance;
  
  
  FILE *debug = fopen("s:\\data\\testdump\\debug.csv", WRITE);
  for (int i = 0; i &lt; threadCount; ++i)
  {
      int processsInBucket = (i == threadCount - 1) ? processsInBundle + processBalance : processBegin[i+1] - processBegin[i];
      fprintf(debug, "%d,start,%d,stop,%d,processsInBucket,%d\n", activeProcessCount, processBegin[i], processEnd[i], processsInBucket);
  }
  fclose(debug);
  </code></pre>
  
  <p>}</p>