shager
2016-10-14 10:40
采纳率: 0%
浏览 3.9k

c++使用libcurl 多线程的问题

多个请求 easy handle 添加到 multi handle里面,然后执行curl_multi_performance,但是执行到一半的时候发生网络故障,但是函数还是执行成功,且running_handle参数一直不变,请教如何判断哪些请求失败了。
代码片段如下:
...
CURLMcode curMCode;

CURLM * curl_m = curl_multi_init();

const int reqNum = 1000;

CURL* curlArr[reqNum];

for (auto& cur : curlArr)
{
    cur = GetKLineReqCUrl();
    curMCode = curl_multi_add_handle(curl_m, cur);
}



int running_handles = 0;
while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(curl_m, &running_handles))
{
        cout << running_handles << endl;
}


do
{
    int numfds = 0;

    CURLMcode _waitResult = curl_multi_wait(curl_m, NULL, 0, 20000, &numfds); // 此函数永远返回成功,发生错误的时候numfds为0

    if (_waitResult != CURLM_OK)
    {
        std::cout << "curl_multi_wait return: " << _waitResult << std::endl;
    }

    curMCode = curl_multi_perform(curl_m, &running_handles); // 发生故障的时候返回值也一直是CURLM_OK,且running_handles一直不减少

} while (running_handles);

std::cout << "curl_multi_perform completed" << std::endl;

int         msgs_left;
CURLMsg *   msg;

while (msg = curl_multi_info_read(curl_m, &msgs_left))
{
    if (CURLMSG_DONE == msg->msg)
    {   
        cout << "recv result: " << msg->data.result << endl;
    }
}

for (auto cur : curlArr)
{
    curl_multi_remove_handle(curl_m, cur);
    curl_easy_cleanup(cur);
}

curl_multi_cleanup(curl_m);

...
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • devmiao 2016-10-16 11:52
    打赏 评论
  • shager 2016-10-17 00:28

    非常感谢您的回答,这样是可以解决了死循环的问题,但是怎么判断是哪些请求失败了呢?

    打赏 评论

相关推荐 更多相似问题