This is my third question about curl_multi_poll, but now I seem to have done everything according to the rules:
#include <iostream>
#include <curl.h>
int main()
{
curl_global_init(CURL_GLOBAL_ALL);
CURLM* CURLM_ = curl_multi_init();
CURL* CURL_ = curl_easy_init();
curl_easy_setopt(CURL_, CURLOPT_URL, "https://stackoverflow.com");
int num_desc_events;
int running_now;
curl_multi_add_handle(CURLM_, CURL_);
curl_multi_perform(CURLM_, &running_now);
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;
curl_multi_perform(CURLM_, &running_now);
std::cout << "curl_multi_perform_start" << std::endl;
std::cout << "running_now:" << running_now << std::endl;
}
curl_easy_cleanup(CURL_);
curl_multi_cleanup(CURLM_);
curl_global_cleanup();
}
The console output will be as follows:
curl_multi_poll_start //Begin loop
curl_multi_poll_awakened
num_desc_events:1 //curl_multi_poll awakened - and the number of sockets on which the event occurred is indicated
curl_multi_perform_start //If there are sockets on which events have occurred, I run curl_multi_perform
running_now:1 //number of sockets still in operation
curl_multi_poll_start
curl_multi_poll_awakened
num_desc_events:1
curl_multi_perform_start
running_now:1
curl_multi_poll_start
curl_multi_poll_awakened
num_desc_events:0 //Attention!!! Why does curl_multi_poll wake up if the number of sockets on which events have occurred is 0?
curl_multi_perform_start
running_now:1
curl_multi_poll_start
curl_multi_poll_awakened
num_desc_events:1
curl_multi_perform_start
running_now:1
curl_multi_poll_start
curl_multi_poll_awakened
num_desc_events:0 //The same
curl_multi_perform_start
running_now:1
curl_multi_poll_start
curl_multi_poll_awakened
num_desc_events:1
curl_multi_perform_start
running_now:1
curl_multi_poll_start
curl_multi_poll_awakened
num_desc_events:1
curl_multi_perform_start
running_now:0 //the number of sockets in operation is 0. Request completed.
curl_multi_poll_start //curl_multi_poll pending
From curl_multi_poll description: https://curl.se/libcurl/c/curl_multi_poll.html
On completion, if numfds is non-NULL, it will be populated with the total number of file descriptors on which interesting events occurred.
Actually the question is: why did curl_multi_poll wake up twice if there were no events on the socket?
CodePudding user response:
As is explained in the documentation, curl_multi_poll() can return "early" without any socket activities when libcurl has "other stuff" to do. Most notably things that are based on timers or timeouts.
Sometimes enabling CURLOPT_VERBOSE and watching that output helps explain what it does at a specific moment in time.
