title: Issues building a multi threaded C++ server
body: <p>Currently, I am trying to explore more about <a href="https://github.com/libevent/libevent" rel="nofollow noreferrer">libevent server library</a>.</p>
  <p>I came across this <a href="https://github.com/frknyldz/http-server-library" rel="nofollow noreferrer">example</a><sup>†</sup>, which I ran with main as:</p>
  <p><sup>†</sup> <a href="https://github.com/frknyldz/http-server-library" rel="nofollow noreferrer">Program to run after downloading libevent library</a></p>
  <h3>main.cpp</h3>
  <pre><code>#include &lt;winsock2.h&gt;
  #include &lt;windows.h&gt;
  #include&lt;stdio.h&gt;
  #include &lt;csignal&gt;
  #include &lt;condition_variable&gt;
  #include &quot;HttpServer.h&quot;
  
  #pragma comment(lib,&quot;WS2_32&quot;)
  
  std::mutex m;
  std::condition_variable cv;
  
  void signalHandler(int signum) 
  {
      cv.notify_all();
  }
  
  void onHello(CHttpRequest *req, void *arg) 
  {
      char *data = &quot;&lt;html&gt;&lt;body&gt;&lt;center&gt;&lt;h1&gt;Hello World &lt;/h1&gt;&lt;/center&gt;&lt;/body&gt;&lt;/html&gt;&quot;;
      req-&gt;AddBufferOut(data, strlen(data));
      req-&gt;SendReply(HTTP_OK);
  }
  
  int main(int argc, char** argv)
  {
      WORD wVersionRequested;
      WSADATA wsaData;
  
      wVersionRequested = MAKEWORD(2, 2);
      WSAStartup(wVersionRequested, &amp;wsaData);
  
      signal(SIGINT, signalHandler);
      signal(SIGTERM, signalHandler);
      CHttpServer server(&quot;127.0.0.1&quot;, 5555, 5); //Create five threads
      server.SetCallback(&quot;/hello&quot;, onHello, NULL);
      server.Start();
      std::unique_lock&lt;std::mutex&gt; lk(m);
      cv.wait(lk);
      return 0;
  }
  </code></pre>
  <p>5 threads get created in this by passing in <code>CHttpServer server(&quot;127.0.0.1&quot;, 5555, 5);</code>. I added a Sleep in <code>void CHttpRequest::AddBufferOut(const char* data, size_t len)</code> of 5 seconds, as shown below:</p>
  <pre><code>evbuffer_add(evhttp_request_get_output_buffer(this-&gt;req), data, len);
  std::this_thread::sleep_for(std::chrono::milliseconds(5000)); // In place of  &quot;evthread_use_pthreads&quot; replaced with &quot;evthread_use_windows_threads&quot; 
  </code></pre>
  <h3>console text</h3>
  <pre class="lang-none prettyprint-override"><code>Thread-id created in Lambda Expression  -----&gt;     1696
  Thread-id created in Lambda Expression  -----&gt;     3008
  Thread-id created in Lambda Expression  -----&gt;     4464
  Thread-id created in Lambda Expression  -----&gt;     5516
  Thread-id created in Lambda Expression  -----&gt;     1652
  Sending Reply by Thread-id------&gt; 3008
  Sending Reply by Thread-id------&gt; 5516
  Sending Reply by Thread-id------&gt; 1696
  Sending Reply by Thread-id------&gt; 4464
  Sending Reply by Thread-id------&gt; 1652
  Sending Reply by Thread-id------&gt; 3008
  Sending Reply by Thread-id------&gt; 3008
  Sending Reply by Thread-id------&gt; 3008
  Sending Reply by Thread-id------&gt; 3008
  Sending Reply by Thread-id------&gt; 3008
  </code></pre>
  <p>The first five requests run parallel, but after that all requests get assigned to same thread. This case occurs when the sleep is more than 1 second, and doesn't when it is less one second. Any help would be appreciated. I am using windows 7 VS 2015.
  I am passing 10 requests using JMeter all request reach at once. It only prints &quot;Hello world&quot; on web <code>http://127.0.0.1:5555/hello</code> so time taken is not much. You can share your experience with a similar bug even if it was encountered in different Programs and measures you had taken.</p>