Libcurl - curl_multi_poll + curl_multi_add_handle - never waits

Viewed 78

The curl_multi_poll function in conjunction with curl_multi_add_handle - for some reason it never waits for an event and immediately returns:

#include <iostream>
#include <thread>
#include <chrono>
#include <curl.h>   





void my_Infinity_loop(CURLM* CURLM_)
{

    int num_desc_events;
    CURL* CURL_ = curl_easy_init();


    while (1)
    {
       std::cout << "curl_multi_poll_start" << std::endl;

       curl_multi_poll(CURLM_, NULL, 0, 1000000, &num_desc_events); 

       std::cout << "curl_multi_poll_awakened" << std::endl;
       std::cout << "num_desc_events:" << num_desc_events << std::endl; //always - 0.

       curl_multi_add_handle(CURLM_, CURL_);    //If this line is deleted, then curl_multi_poll will wait again

    }

}



void my_wakeup_func(CURLM* CURLM_)
{
    std::this_thread::sleep_for(std::chrono::seconds(5));           

    curl_multi_wakeup(CURLM_);

    std::cout << "curl_multi_wakeup_START" << std::endl;
}






int main()
{
    

    curl_global_init(CURL_GLOBAL_ALL); 

    CURLM* CURLM_ = curl_multi_init();


    std::thread thread_1 (my_Infinity_loop, CURLM_);

    std::thread thread_2(my_wakeup_func, CURLM_);





    thread_1.join();
    thread_2.join();

    curl_multi_cleanup(CURLM_);
    curl_global_cleanup();
}

-That is, I create a thread in which I run an infinite loop - with curl_multi_poll waiting for events on a multi descriptor.

-In the second thread, I run the function - waiting for 5 seconds and after that - the function wakes up curl_multi_poll through the curl_multi_wakeup function.

-curl_multi_poll wakes up and the loop calls the curl_multi_add_handle function. After that, the loop goes back to the curl_multi_poll function, BUT curl_multi_poll - ends immediately, it does not wait for events. I don't understand why.

-However, if you remove the curl_multi_add_handle function from the loop, then everything is fine and the curl_multi_poll function will go into standby mode again.

What am I missing ?

0 Answers
Related