title: Multi-Threading in C++ throws thread constructor failed: Resource temporarily unavailable
body: <p>Edited: </p>
  
  <p>I'm learning multi-threading with Matrix Multiplication as an example and I have created this program:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;thread&gt;
  #include &lt;functional&gt;
  
  using namespace std;
  
  int N = 50;
  void do_multiply_for_row(const vector&lt;vector&lt;int&gt; &gt;&amp; matrix, int i, int N, vector&lt;int&gt;&amp; answer) {
      for (int j = 0; j &lt; N; j++) {
          answer[j] = 0;
          for (int k = 0; k &lt; N; k++) {
              answer[j] += matrix[i][k] * matrix[k][j];
          }
      }
      cout &lt;&lt; "Done " &lt;&lt; i &lt;&lt;  endl;
  }
  
  int main() {
      vector&lt;vector&lt;int&gt; &gt; matrix(N, vector&lt;int&gt; (N, 0));
      int x = 0;
      for (int i = 0; i &lt; N; i++) {
          for (int j = 0; j &lt; N; j++) {
              matrix[i][j] = x++;
          }
      }
      vector&lt;vector&lt;int&gt; &gt; result(N, vector&lt;int&gt; (N, 0));
      vector&lt;std::thread&gt; threads;
      for (int i = 0; i &lt; N; i++) {
              threads.push_back(std::thread(std::bind(do_multiply_for_row, std::cref(matrix), i, N, std::ref(result[i]))));
      }
      for (int i = 0; i &lt; threads.size(); i++) threads[i].join();
      for (int i = 0; i &lt; N; i++) {
          for (int j =0; j &lt; N; j++){
              cout &lt;&lt; result[i][j] &lt;&lt; " ";
          }
          cout &lt;&lt; endl;
      }
      return 0;
  }
  </code></pre>
  
  <p>I'll end up creating 50 threads but got <code>libc++abi.dylib: terminating with uncaught exception of type std::__1::system_error: thread constructor failed: Resource temporarily unavailable</code></p>
  
  <p>Is there a limit on number of threads i create or i'm doing something wrong?</p>