title: C++ Multithreading on Multicore system
body: <p>When running the following code on a four core CPU, am I guaranteed these four threads are going to be run on four difference cores? </p>
  
  <p>Or the OS could put multiple threads on a single core depending on the workload of the system. If this is the case, running multiple-thread may even give you performance penalties due to context-switching and other threading overhead. </p>
  
  <p>I suppose there is no way to code the application, which forces the threads to be run on different cores?    </p>
  
  <pre><code>int main()
  {
      std::vector&lt;std::thread&gt; threads(3);
  
      for (size_t i = 0; i &lt; 3; i++)
      {
          threads[i] = std::thread(DoSomethingLengthy);
      }
  
      DoSomethingLengthy();
  
      for (auto&amp; thread : threads)
      { 
          thread.join(); 
      }
  }
  </code></pre>