title: Reading m words from a paragraph from n thread ? C++ multithreading ? Not working?
body: <p>I want read a paragraph by extracting one word at a time using Multithreading . Each thread should read exactly one word and when paragraph ends they should exit peacefully . I know threads shouldn't be used in this way as there is no advantage in that . But I want to do that so that I can check how threads can work sequentially if required . I tried but it looks like the program is reaching deadlock state and not giving any output at all . There are 11 words in the string and I am using 4 threads .</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;mutex&gt;
  #include &lt;sstream&gt;
  #include &lt;thread&gt;
  #include &lt;chrono&gt;
  #include &lt;condition_variable&gt;
  using namespace std;
  stringstream s("Japan US Canada UK France Germany China Russia Korea India Nepal");
  int count = 0;
  string word;
  condition_variable cv;
  mutex m;
  int i = 0;
  bool check_func(int i,int k)
  {
      return i == k;
  }
  void print(int k)
  {
    while(count &lt; 11)   // As there are 11 words
    {
       unique_lock&lt;mutex&gt; lk(m);
       int z = k;
       cv.wait(lk,[&amp;]{return check_func(i,z);});            // Line 33
       s &gt;&gt; word;
       cout&lt;&lt;word&lt;&lt;" ";
       i++;
       cv.notify_all();
       count++;
    }
     return;
  }
  int main() 
  {
      thread threads[4];
      for(int i = 0; i &lt; 4; i++)
         threads[i] = thread(print,i);
      for(auto &amp;t : threads)
         t.join();
      return 0;
  }
  </code></pre>