title: LibCurl parallel http request in a multi threaded environment
body: <p>Currently, I am creating a pool for curl easy handle to make parallel HTTP requests. But this is blocking.</p>
  <p>Here is my implementation:</p>
  <pre><code>class CurlHttpClient
  {
  public:
      ...
  
  private:
      CURL* m_curl = nullptr;
      std::mutex m_mtx;
      std::stack&lt;CURL*&gt; m_poolCurlHandle;
      CURL* getCurlHandleFromPool();
      void pushCurlHandle(CURL* curlHandle);
  };
  
  CURL* CurlHttpClient::getCurlHandleFromPool()
  {
      CURL* pCurlHandle = nullptr;
      std::scoped_lock lock(m_mtx);
      {
          if (!m_poolCurlHandle.empty())
          {
              pCurlHandle = m_poolCurlHandle.top();
              m_poolCurlHandle.pop();
          }
          return pCurlHandle;
      }
      return curl_easy_duphandle(m_curl);
  }
  
  
  void CurlHttpClient::pushCurlHandle(CURL* curlHandle)
  {
      std::scoped_lock lock(m_mtx);
      m_poolCurlHandle.push(curlHandle);
  }
  
  int CurlHttpClient::request(const std::string&amp; requestUrl, std::string&amp; responseBuffer)
  {
      
      CURL* curlHandle = getCurlHandleFromPool();
      
      curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, (void*)&amp;responseBuffer);
      curl_easy_setopt(curlHandle, CURLOPT_URL, requestUrl.c_str());
  
      CURLcode res = curl_easy_perform(curlHandle);
      
      int httpResponseCode = 0;
      curl_easy_getinfo(curlHandle, CURLINFO_RESPONSE_CODE, &amp;httpResponseCode);
      pushCurlHandle(curlHandle);
      return httpResponseCode;
  }
  
  
  // Example Client
  int main()
  {
  CurlHttpClient client;
  ...
  std::thread t1([&amp;] { std::string response; std::string url;  client.request(url, response); //do something with response});
  std::thread t2([&amp;] { std::string response; std::string url;  client.request(url, response); //do something with response});
  std::thread t3([&amp;] { std::string response; std::string url;  client.request(url, response); //do something with response});
  std::thread t4([&amp;] { std::string response; std::string url;  client.request(url, response); //do something with response});
  
  t1.join();
  t2.join();
  t3.join();
  t4.join();
  }
  </code></pre>
  <p>Here you could see that I am returning responses individually for each request as soon as it is received.</p>
  <p>I read that curl multi-interface is better in handling the parallel request. I have seen some examples of multi-interface setup and all the examples are accumulating the request and firing the multi perform. But I am not sure how to return the data immediately to the client when an individual response is received and keep waiting for another response to arrive.</p>
  <p>Is multi-interface helpful in my particular scenario? If so how can I return an individual response immediately when it is received?</p>