同步的 curl 请求

Synchronized curl requests

我正在尝试向多个目标发出 HTTP 请求,我需要它们 运行(几乎)完全在同一时刻。

我试图为每个请求创建一个线程,但我不知道为什么 Curl 在执行时会崩溃。我使用的是每个线程的简单处理,所以理论上一切都应该没问题...

有没有人遇到过类似的问题?或有谁知道多界面是否允许您选择何时执行所有请求?

非常感谢。

编辑:

代码示例如下:

void Clazz::function(std::vector<std::string> urls, const std::string& data)
{
    for (auto it : urls)
    {
        std::thread thread(&Clazz::DoRequest, this, it, data);
        thread->detach();
    }
}

int Clazz::DoRequest(const std::string& url, const std::string& data)
{
    CURL* curl = curl_easy_init();
    curl_slist *headers = NULL;

    headers = curl_slist_append(headers, "Expect:"); 
    headers = curl_slist_append(headers, "Content-Type: application/json");

    curl_easy_setopt(curl, CURLOPT_POST, 1);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());

    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 15);
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
    curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1L);

    //curlMutex.lock();
    curl_easy_perform(curl);
    //curlMutex.unlock();
    long responseCode = 404;
    curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &responseCode);

    curl_easy_cleanup(curl);
    curl_slist_free_all(headers);
}

希望对您有所帮助,谢谢!

您在任何地方打电话给 curl_global_init 吗?也许在您的 main() 方法的早期?

引用自http://curl.haxx.se/libcurl/c/curl_global_init.html

This function is not thread safe. You must not call it when any other thread in the program (i.e. a thread sharing the same memory) is running. This doesn't just mean no other thread that is using libcurl. Because curl_global_init calls functions of other libraries that are similarly thread unsafe, it could conflict with any other thread that uses these other libraries.

引用自http://curl.haxx.se/libcurl/c/curl_easy_init.html

If you did not already call curl_global_init, curl_easy_init does it automatically. This may be lethal in multi-threaded cases, since curl_global_init is not thread-safe, and it may result in resource problems because there is no corresponding cleanup.

听起来你没有打电话给 curl_global_init,而是让 curl_easy_init 为你处理。由于您同时在两个线程上执行此操作,因此您遇到了线程不安全的情况,并产生了上述的致命结果。

能够在设备中正确调试后,您发现问题是 curl 的一个老问题。

http://curl.haxx.se/mail/lib-2010-11/0181.html

在每个 curl 句柄中使用 CURLOPT_NOSIGNAL 后,崩溃就消失了。 :)