libCurl真的是线程安全的吗?

时间:2015-05-07 10:13:41

标签: multithreading c++11 valgrind libcurl

在找不到previous question中我的应用程序中发生的事情之后,我尝试使用valgrind在我的电脑中重现它以查找内存/多线程问题。我在很多地方看到过:

==769== Possible data race during write of size 4 at 0xACD3ADC by thread #13
==769== Locks held: none
==769==    at 0x4C36067: memset (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==769==    by 0x4E4570D: pthread_create@@GLIBC_2.2.5 (allocatestack.c:249)
==769==    by 0x4C30C90: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==769==    by 0x509F957: Curl_thread_create (in /usr/lib/x86_64-linux-gnu/libcurl.so.4.3.0)
==769==    by 0x50A261B: Curl_resolver_getaddrinfo (in /usr/lib/x86_64-linux-gnu/libcurl.so.4.3.0)
==769==    by 0x5066163: Curl_resolv (in /usr/lib/x86_64-linux-gnu/libcurl.so.4.3.0)
==769==    by 0x507D74D: Curl_connect (in /usr/lib/x86_64-linux-gnu/libcurl.so.4.3.0)
==769==    by 0x508D62F: multi_runsingle (in /usr/lib/x86_64-linux-gnu/libcurl.so.4.3.0)
==769==    by 0x508E180: curl_multi_perform (in /usr/lib/x86_64-linux-gnu/libcurl.so.4.3.0)
==769==    by 0x50857B2: curl_easy_perform (in /usr/lib/x86_64-linux-gnu/libcurl.so.4.3.0)
==769==    by 0x489D63: ConnectionMgr::checkInternetConnection() (ConnectionMgr.cpp:287)
==769==    by 0x490E9E: void std::_Mem_fn<void (ConnectionMgr::*)()>::operator()<, void>(ConnectionMgr*) const (in /home/user/app)
==769== 
==769== Address 0xACD3ADC is 44 bytes inside a block of size 304 alloc'd
==769==    at 0x4C2DFF0: calloc (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==769==    by 0x4012E54: _dl_allocate_tls (dl-tls.c:296)
==769==    by 0x4E45DA0: pthread_create@@GLIBC_2.2.5 (allocatestack.c:589)
==769==    by 0x4C30C90: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==769==    by 0x509F957: Curl_thread_create (in /usr/lib/x86_64-linux-gnu/libcurl.so.4.3.0)
==769==    by 0x50A261B: Curl_resolver_getaddrinfo (in /usr/lib/x86_64-linux-gnu/libcurl.so.4.3.0)
==769==    by 0x5066163: Curl_resolv (in /usr/lib/x86_64-linux-gnu/libcurl.so.4.3.0)
==769==    by 0x507D74D: Curl_connect (in /usr/lib/x86_64-linux-gnu/libcurl.so.4.3.0)
==769==    by 0x508D62F: multi_runsingle (in /usr/lib/x86_64-linux-gnu/libcurl.so.4.3.0)
==769==    by 0x508E180: curl_multi_perform (in /usr/lib/x86_64-linux-gnu/libcurl.so.4.3.0)
==769==    by 0x50857B2: curl_easy_perform (in /usr/lib/x86_64-linux-gnu/libcurl.so.4.3.0)
==769==    by 0x489D63: ConnectionMgr::checkInternetConnection() (ConnectionMgr.cpp:287)

我在一个分离的线程中运行的函数就是这个。

void ConnectionMgr::checkInternetConnection()
{
    CURL* curl = curl_easy_init();

    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 15);
    curl_easy_setopt(curl, CURLOPT_URL,"http://www.google.com");
    curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1L);
    curl_easy_setopt (curl, CURLOPT_CONNECT_ONLY, 1L);

    long responseCode = 404;
    curl_easy_perform(curl);
    curl_easy_getinfo (curl, CURLINFO_HTTP_CONNECTCODE, &responseCode);

    curl_easy_cleanup(curl);

    mHasInternet = (responseCode == 200);
}

非常感谢。

2 个答案:

答案 0 :(得分:5)

是!

libcurl在documented restrictions下是线程安全的。数以百计的应用程序每天都以线程方式成功使用它。

有没有错误?当然,我们努力修复我们报告给项目的所有人。

答案 1 :(得分:3)

也许这个问题有点过时,但我找到了答案。问题是Curl正在使用信号通知请求超时,并且似乎无法在多线程中正常工作。

为了解决这个问题,我使用了 curl_easy_setopt(curl,CURLOPT_NOSIGNAL,1); ,之后它运行正常。