title: Multiprocessor Boost::Thread? All threads running on one processor
body: <p>I have a embarrassingly parallel problem that I want to execute on multiple processors.  I had supposed that <code>boost::thread</code> would automatically send new threads to new processors, but all of them are executing on the same core as the parent process.  Is it possible to get each thread to run on a different processor, or do I need something like MPI? </p>
  
  <p>My suspicion is that <code>boost::thread</code> is simply not a multi-processor tool, that I'm asking it to do something it's not designed for.</p>
  
  <p>EDIT: my question boils down to this: Why do all the threads execute on one processor?  Is there a way to get <code>boost::thread</code> to send threads to different processors?</p>
  
  <p>Here's the relevant sample of my code:</p>
  
  <pre><code>size_t lim=1000;
  std::deque&lt;int&gt; vals(lim);
  std::deque&lt;boost::thread *&gt; threads;
  int i=0; 
  std::deque&lt;int&gt;::iterator it = vals.begin();
  for (; it!=sigma.end(); it++, i++) {
    threads.push_back(new boost::thread(doWork, it, i));
    while (threads.size() &gt;= maxConcurrentThreads) {
      threads.front()-&gt;join();
      delete threads.front();
      threads.pop_front();
    }
  }
  while(threads.size()) {
    threads.front()-&gt;join();
    threads.pop_front();
  }
  </code></pre>
  
  <p>As should be clear, <code>doWork</code> does some calculation using the parameter <code>i</code> and stores the result in <code>vals</code>.  My idea was that setting <code>maxConncurrentThreads</code> to be equal to the number of cores available, and then each thread would use the core that was idle.  I just need someone to confirm that <code>boost::thread</code> cannot be made to work in this way.</p>
  
  <p>(I'd guess that there's a better way to limit the number of concurrent threads than using a queue; feel free to scold me for that as well.)</p>
  
  <hr>
  
  <p>Here's the <code>doWork</code> function:</p>
  
  <pre><code>void doWork(std::deque&lt;int&gt;::iterator it, int i) {
    int ret=0;
    int size = 1000; // originally 1000, later changed to 10,000,000
    for (int j=i; j&lt;i+size; j++) {
      ret+=j;
    }
    *it=ret;
    return;
  }
  </code></pre>
  
  <hr>
  
  <p>EDIT: As Martin James suggested, the problem was that the doWork function was initially only 1000 int additions.  With such a small job, scheduling the thread took longer than executing the thread, so only one processor was in use.  Making the job longer (adding 10,000,000 ints) yielded the desired behavior.  The point being: <code>boost::thread</code> <em>will</em> use multiple cores by default, but if your threads do less work than scheduling the thread then you won't see any benefit from multithreading.</p>
  
  <p>Thanks to everyone for aiding my understanding in this.</p>