title: Thread inside another thread in a while loop
body: <p>I'm pretty new to C++ and I'm experimenting with threads right now. 
  I'm trying to create a thread inside a thread under a while loop. But I don't think it seems to be working.
  Presently my code looks like this:</p>
  
  <pre><code>#include &lt;&gt;
  std::vector&lt;pthread_t&gt; outer_thread, inner_thread;
  
      void *inner_thread(void *ptr)
      {
        string data1;
        data1 = *(reinterpret_cast&lt;string*&gt;(ptr));
        cout &lt;&lt; "inner thread started " &lt;&lt; data1;
  
  /* do something */
        cout &lt;&lt; "inner thread stopped " &lt;&lt; data1;
  
  pthread_exit(NULL);
    return 0;
  
  
      }
  
      void *outer_thread(void *ptr )
      {
  cout &lt;&lt; "out thread started" &lt;&lt; endl;
  //cout &lt;&lt; ptr &lt;&lt; endl;
  //cout &lt;&lt; *(reinterpret_cast&lt;string*&gt;(ptr)) &lt;&lt; endl;
  string data;
  data = *(reinterpret_cast&lt;string*&gt;(ptr));
  
  
        string str3;
  while (getline(data,str3))
  {
        cout &lt;&lt; "out thread started" &lt;&lt; endl;
  
  
  pthread_t in_thread;
  in_vec.push_back(str3);
                  int create_thread2 = pthread_create(&amp;in_thread, NULL, &amp;inner_thread, reinterpret_cast&lt;void*&gt;(&amp;(in_vec.at(j))));
                  inner_thread.push_back(in_thread);
  
  
        if (create_thread2 != 0) 
          cout &lt;&lt; "Error : Thread";
  
  
        j++;
  
        cout &lt;&lt; "out thread ends " &lt;&lt; j &lt;&lt; create_thread2 &lt;&lt; endl ;
  
      }
           for (int k = 0; k &lt; j ; k++)
  
  {
  pthread_join(inner_thread.at(k),NULL) ;
  }
  
  pthread_exit(NULL);
    return 0;
  
  }   
      int main (int argc, char *argv[])
      {
        int i = 0;
        while (getline(gin,str))
        {
   string str1;
                  pthread_t out_thread;
                  cout &lt;&lt; "str1" &lt;&lt; str1 &lt;&lt; endl;
  now_vec.push_back(str1);
  int create_thread = pthread_create(&amp;out_thread, NULL, &amp;outer_thread, reinterpret_cast&lt;void*&gt;(&amp;(now_vec.at(i))));
                  outer_thread.push_back(out_thread);
                  if (create_thread != 0) cout &lt;&lt; "Error : Thread" ;
  
          i++;
        }
  
  for (int k = 0 ; k &lt; i; k ++)
  {
  cout &lt;&lt; i &lt;&lt; endl;
  //cout &lt;&lt; "third thread " &lt;&lt; outer_thread.at(1) &lt;&lt; endl;
  cout &lt;&lt; outer_thread.at(k) &lt;&lt; endl;
  cout &lt;&lt; "out out out" &lt;&lt; endl;
  pthread_join(outer_thread.at(k),NULL) ;
  }
  
  
      }
  </code></pre>
  
  <p>I'm trying to read a file which contains the list of files that should be read. I want to read all these files simultaneously. 
  All these files contain information and needs another set of threads to start another operation. So this also needs to be done simultaneously. 
  That's the reason I have 2 sets of threads running.
  Let me know If there is any faster and simpler way to do this?</p>
  
  <p>It seems to wait till the inner thread finishes and then starts with the next iteration. I want the inner threads to run simultaneously inside the outer thread. May I know how to go about this?</p>