title: C++ multithreads run time issue
body: <p>I have been studying C++ multithreads and get a question about it. </p>
  
  <p>Here is what I am understanding about multithreads.
  One of the reasons we use multithreads is to reduce the run time, right? 
  For example, I think if we use two threads we can expect half of the execution time. 
  So, I tried to code to prove it. 
  Here is the code. </p>
  
  <pre><code>#include &lt;vector&gt;
  #include &lt;iostream&gt;
  #include &lt;thread&gt;
  #include &lt;future&gt;
  
  using namespace std;
  #define iterationNumber 1000000
  
  void myFunction(const int index, const int numberInThread, promise&lt;unsigned long&gt;&amp;&amp; p, const vector&lt;int&gt;&amp; numberList) { 
      clock_t begin,end;
      int firstIndex = index * numberInThread;
      int lastIndex = firstIndex + numberInThread;
      vector&lt;int&gt;::const_iterator first = numberList.cbegin() + firstIndex;
      vector&lt;int&gt;::const_iterator last = numberList.cbegin() + lastIndex;
  
      vector&lt;int&gt; numbers(first,last);
  
      unsigned long result = 0;
  
      begin = clock();
      for(int i = 0 ; i &lt; numbers.size(); i++) {
          result += numbers.at(i);
      }
      end = clock();
      cout &lt;&lt; "thread" &lt;&lt; index &lt;&lt; " took " &lt;&lt; ((float)(end-begin))/CLOCKS_PER_SEC &lt;&lt; endl;
  
      p.set_value(result);
  
  }
  
  
  int main(void)
  {
      vector&lt;int&gt; numberList;
      vector&lt;thread&gt; t;
      vector&lt;future&lt;unsigned long&gt;&gt; futures;
      vector&lt;unsigned long&gt; result;
      const int NumberOfThreads = thread::hardware_concurrency() ?: 2;
      int numberInThread = iterationNumber / NumberOfThreads;
  
      clock_t begin,end;
  
  
      for(int i = 0 ; i &lt; iterationNumber ; i++) {
          int randomN =  rand() % 10000 + 1;
          numberList.push_back(randomN);
      }
  
      for(int j = 0 ; j &lt; NumberOfThreads; j++){
          promise&lt;unsigned long&gt; promises;
          futures.push_back(promises.get_future());
          t.push_back(thread(myFunction, j, numberInThread, std::move(promises), numberList));
      }
  
      for_each(t.begin(), t.end(), std::mem_fn(&amp;std::thread::join));
  
      for (int i = 0; i &lt; futures.size(); i++) {
          result.push_back(futures.at(i).get());
      }
  
      unsigned long RRR = 0;
  
      begin = clock();
      for(int i = 0 ; i &lt; numberList.size(); i++) {
          RRR += numberList.at(i);
      }
      end = clock();
      cout &lt;&lt; "not by thread took " &lt;&lt; ((float)(end-begin))/CLOCKS_PER_SEC &lt;&lt; endl;
  
  }
  </code></pre>
  
  <p>Because the hardware concurrency of my laptop is 4, it will create 4 threads and each takes a quarter of numberList and sum up the numbers. </p>
  
  <p>However, the result was different than I expected. </p>
  
  <pre><code>thread0 took 0.007232
  thread1 took 0.007402
  thread2 took 0.010035
  thread3 took 0.011759
  not by thread took 0.009654
  </code></pre>
  
  <p>Why? Why it took more time than serial version(not by thread). </p>