title: How to do curl_multi_perform() asynchronously in C++?
body: <p>I have come to use <code>curl</code> synchronously doing a http request. My question is how can I do it asynchronously?</p>
  
  <p>I did some searches which lead me to the documentation of <code>curl_multi_*</code> interface from this <a href="https://stackoverflow.com/questions/15311667/is-curl-easy-perform-synchronous-or-asynchronous">question</a> and this <a href="http://www.godpatterns.com/2011/09/asynchronous-non-blocking-curl-multi.html" rel="noreferrer">example</a> but it didn't solve anything at all.</p>
  
  <p>My simplified code:</p>
  
  <pre><code>CURLM *curlm;
  int handle_count = 0;
  curlm = curl_multi_init();
  
  CURL *curl = NULL;
  curl = curl_easy_init();
  
  if(curl)
  {
      curl_easy_setopt(curl, CURLOPT_URL, "https://stackoverflow.com/");
      curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
      curl_multi_add_handle(curlm, curl);
      curl_multi_perform(curlm, &amp;handle_count);
  }
  
  curl_global_cleanup();
  </code></pre>
  
  <p>The callback method <code>writeCallback</code> doesn't get called and nothing happens.</p>
  
  <p>Please advise me.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>According to @Remy's below answer, I got this but seems that it's not quite what I really needed. Cause using a loop is still a blocking one. Please tell me if I'm doing wrong or misunderstanding something. I'm actually pretty new to C++.</p>
  
  <p>Here's my code again:</p>
  
  <pre><code>int main(int argc, const char * argv[])
  {
      using namespace std;
      CURLM *curlm;
      int handle_count;
      curlm = curl_multi_init();
  
      CURL *curl1 = NULL;
      curl1 = curl_easy_init();
  
      CURL *curl2 = NULL;
      curl2 = curl_easy_init();
  
      if(curl1 &amp;&amp; curl2)
      {
          curl_easy_setopt(curl1, CURLOPT_URL, "https://stackoverflow.com/");
          curl_easy_setopt(curl1, CURLOPT_WRITEFUNCTION, writeCallback);
          curl_multi_add_handle(curlm, curl1);
  
          curl_easy_setopt(curl2, CURLOPT_URL, "http://google.com/");
          curl_easy_setopt(curl2, CURLOPT_WRITEFUNCTION, writeCallback);
          curl_multi_add_handle(curlm, curl2);
  
          CURLMcode code;
          while(1)
          {
              code = curl_multi_perform(curlm, &amp;handle_count);
  
              if(handle_count == 0)
              {
                  break;
              }
          }
      }
  
      curl_global_cleanup();
  
      cout &lt;&lt; "Hello, World!\n";
      return 0;
  }
  </code></pre>
  
  <p>I can now do 2 http requests simultaneously. Callbacks are called but still need to finish before executing following lines. Will I have to think of thread?</p>