title: Is it safe to create new thread in a loop?
body: <p>Is it safe to create new thread inside while loop? I have tried this way:</p>
  
  <pre><code>std::thread thread(ClientLoop,clientSocket)
  </code></pre>
  
  <p>But as soon as the function return it throws the error.</p>
  
  <pre><code>while (true){
      cout &lt;&lt; "Waiting for new connections" &lt;&lt; endl;
      clientSocket = accept(listenSocket, nullptr, nullptr);
      cout &lt;&lt; "Client connected" &lt;&lt; endl;
      new thread(ClientLoop,clientSocket);                
  }   
  </code></pre>
  
  <p>This way it works, but I wonder if there are no memory leaks. Thanks.</p>