title: Threads dead lock (I think)
body: <p>I am new to multithreading in c++.</p>
  
  <p>What I want to do is to first create 2 threads. All threads will wait until they get a item to a set.I am using array of mutexes (For making threads to wait).After waiting they perform task(extract array). They will increase count to let main thread know that they extracted array. In this way after processing all three arrays I want my threads to print size of their extracted array and exit. But unfortunately my code is not getting terminated. I think it went into deadlock.</p>
  
  <p>Can anyone please help.</p>
  
  <p>This is my code:</p>
  
  <pre><code>#include &lt;bits/stdc++.h&gt;
  #include&lt;thread&gt;
  #include &lt;chrono&gt;  
  #include&lt;mutex&gt;
  #include&lt;condition_variable&gt;
  int n=5;
  int count=0;
  std::vector&lt;std::vector&lt;std::string&gt;&gt; sets;
  std::vector&lt;std::vector&lt;std::string&gt;&gt; x;
  std::mutex m[2],as,p;
  std::condition_variable cv,cb;
  int y=0,rt[2]={0};
  int flag[2][3]={0};
  int u=0;
  
  void see(int i)
  {
      //cout&lt;&lt;"hii";
      int j=0;
      while(j&lt;3)
      {
  
  
          std::unique_lock&lt;std::mutex&gt; lk(m[i]);
          std::cout&lt;&lt;"\nwaiting\n"&lt;&lt;"i="&lt;&lt;i&lt;&lt;"set size="&lt;&lt;sets.size()&lt;&lt;"u="&lt;&lt;u&lt;&lt;" flag="&lt;&lt;flag[i][u]&lt;&lt;"\n";
  
          cv.wait(lk, [i]{return (sets.size()&gt;0&amp;&amp;flag[i][u]==0)?true:false;});
          std::cout&lt;&lt;"came out i="&lt;&lt;i&lt;&lt;"\n";
          flag[i][u]=1;
          for(int qw=0;qw&lt;4;qw++)
          {
           x[i].push_back(sets[0][qw]);   
          }
          lk.unlock();
          std::unique_lock&lt;std::mutex&gt; lq(as);
          count++;
          cb.notify_one();
          lq.unlock();
  
  
          j++;
  
  
  
  
  
      }
      std::cout&lt;&lt;"size of map of "&lt;&lt;i&lt;&lt;"is "&lt;&lt;x[i].size()&lt;&lt;"\n";
      for(int w=0;w&lt;12;w++)
      {
          std::cout&lt;&lt;x[i][w]&lt;&lt;" ";
  
      }
      std::cout&lt;&lt;"\n";
  
  
      return;
  }
  
  int main()
  {
  
      std::vector&lt;std::vector&lt;std::string&gt;&gt; r;
      for(int i=0;i&lt;2;i++)
      {
          std::vector&lt;std::string&gt; e;
          x.push_back(e);
      }
      std::vector&lt;std::thread&gt; threads;
      std::string s[][4]={
          {"Test","Create","Delete","Add"},
          {"class","object","demo","status"},
          {"Deep_Learning","Neural_Network","Blockchain"}
  
  
      };
  
      while(y&lt;3)
      {
          std::vector&lt;std::string&gt; q;
          for(int i=0;i&lt;4;i++)
          {
          q.push_back(s[y][i]);
          }
          r.push_back(q);
          y++;
  
      }
  
      for(int i=0;i&lt;2;i++)
      {
  
  
          threads.push_back(std::thread(see,i));//creating thread
      }
  
      y=0;
      //Main code starts from below
  
  
  
      while(y&lt;3)
      {
  
              std::cout&lt;&lt;"pushing\n";
              sets.push_back(r[y]);//first passing set 1 to all threads
              cv.notify_all();
              std::unique_lock&lt;std::mutex&gt; lt(p);
              cb.wait(lt, []{return count==2;});
              std::cout&lt;&lt;"main y="&lt;&lt;y&lt;&lt;"\n";
              sets.pop_back();
              u++;
  
  
  
          count=0;
          y++;
  
      }
  
  
  
      for(int i=0;i&lt;2;i++)
      {
  
          threads[i].join();
      }
  
      return 0;
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>waiting                                                                                                                                         
  i=0set size=pushing                                                                                                                             
  
  waiting                                                                                                                                         
  i=1set size=0u=0 flag=0                                                                                                                         
  came out i=1                                                                                                                                    
  
  waiting                                                                                                                                         
  i=1set size=1u=0 flag=1                                                                                                                         
  0u=0 flag=0                                                                                                                                     
  came out i=0                                                                                                                                    
  
  waiting                                                                                                                                         
  i=0set size=1u=0 flag=1                                                                                                                         
  main y=0                                                                                                                                        
  pushing                                                                                                                                         
  came out i=1                                                                                                                                    
  
  waiting                                                                                                                                         
  i=1set size=1u=1 flag=1    
  (Output is not terminating and i think it went into dead lock can anyone help why it is not 
  terminating) 
  </code></pre>