title: Multi-threaded file reading produces the same result for each thread
body: <p>Basically, the issue I am having is in the title, I am trying to create a multi-threaded application to read and sum up the contents of a file, this works correctly with one thread. However, when more are introduced they come out with the same output. How do I fix this?</p>
  
  <p>The code</p>
  
  <pre><code>void *sumThread(void *);
  pthread_mutex_t keepOut = PTHREAD_MUTEX_INITIALIZER;
  pthread_mutex_t keepOutSum = PTHREAD_MUTEX_INITIALIZER;
  int counter = 0, line_count = 0;
  char* loc;
  double total = 0;
  
  void split(const string&amp; s, char c, vector&lt;string&gt;&amp; v)
  {
      string::size_type i = 0;
      string::size_type j = s.find(c);
  
      while (j != string::npos)
      {
          v.push_back(s.substr(i, j - i));
          i = ++j;
          j = s.find(c, j);
  
          if (j == string::npos)
              v.push_back(s.substr(i, s.length()));
      }
  }
  
  int main(int argc, char* argv[])
  {
  
      if (argc &lt; 2)
      {
  
          cerr &lt;&lt; "Usage: " &lt;&lt; argv[0] &lt;&lt; " filename" &lt;&lt; endl;
          return 1;
      }
  
      string line;
      loc = argv[1];
      ifstream myfile(argv[1]);
      myfile.unsetf(ios_base::skipws);
  
      line_count = std::count(std::istream_iterator&lt;char&gt;(myfile),
                              std::istream_iterator&lt;char&gt;(),
                              '\n');
  
      myfile.clear();
      myfile.seekg(-1, ios::end);
      char lastChar;
      myfile.get(lastChar);
      if (lastChar != '\r' &amp;&amp; lastChar != '\n')
          line_count++;
  
      myfile.setf(ios_base::skipws);
      myfile.clear();
      myfile.seekg(0, ios::beg);
  
      pthread_t thread_id[NTHREADS];
  
      for (int i = 0; i &lt; NTHREADS; ++i)
      {
          pthread_create(&amp;thread_id[i], NULL, sumThread, NULL);
      }
  
      for (int i = 0; i &lt; NTHREADS; ++i)
      {
          pthread_join(thread_id[i], NULL);
      }
  
      cout &lt;&lt; setprecision(2) &lt;&lt; fixed &lt;&lt; total &lt;&lt; endl;
      return 0;
  }
  
  void *sumThread(void *)
  {
  
      pthread_mutex_lock(&amp;keepOut);
      int threadNo = counter;
      counter++;
      pthread_mutex_unlock(&amp;keepOut);
  
      ifstream myfile(loc);
      double runningTotal = 0;
      string line;
  
      if (myfile.is_open())
      {
          for (int i = threadNo; i &lt; line_count; i += NTHREADS)
          {
              vector &lt; string &gt; parts;
  
              getline(myfile, line);
              // ... and process out the 4th element in the CSV.
              split(line, ',', parts);
  
              if (parts.size() != 3)
              {
                  cerr &lt;&lt; "Unable to process line " &lt;&lt; i
                          &lt;&lt; ", line is malformed. " &lt;&lt; parts.size()
                          &lt;&lt; " parts found." &lt;&lt; endl;
                  continue;
              }
  
              // Add this value to the account running total.
              runningTotal += atof(parts[2].c_str());
          }
          myfile.close();
      }
      else
      {
          cerr &lt;&lt; "Unable to open file";
      }
  
      pthread_mutex_lock(&amp;keepOutSum);
  
      cout &lt;&lt; threadNo &lt;&lt; ":  " &lt;&lt; runningTotal &lt;&lt; endl;
      total += runningTotal;
      pthread_mutex_unlock(&amp;keepOutSum);
      pthread_exit (NULL);
  }
  </code></pre>
  
  <p>Sample output </p>
  
  <pre><code> 2:  -46772.4
   0:  -46772.4
   1:  -46772.4
   3:  -46772.4
   -187089.72
  </code></pre>
  
  <p>Each thread is supposed to read and sum up the numbers in the file, then add them together when it's done. However, the threads all seem to return the same number even though the threadNo variable a clearly different as indicated in the output.</p>