Libcurl的多线程问题

  1. libcurl的curl_global_init 不是线程安全的

    The basic rule for constructing a program that uses libcurl is this: Call curl_global_init(), with a CURL_GLOBAL_ALL argument, immediately after the program starts, while it is still only one thread and before it uses libcurl at all. Call curl_global_cleanup() immediately before the program exits, when the program is again only one thread and after its last use of libcurl.

  2. 多线程使用libcurl时,所有curl handler必须把CURLOPT_NOSIGNAL设置为1

    http://curl.haxx.se/libcurl/c/libcurl-tutorial.html#Multi-threading

    The first basic rule is that you must never share a libcurl handle (be it easy or multi or whatever) between multiple threads. Only use one handle in one thread at a time.

    libcurl is completely thread safe, except for two issues: signals and SSL/TLS handlers. Signals are used for timing out name resolves (during DNS lookup) - when built without c-ares support and not on Windows.

    If you are accessing HTTPS or FTPS URLs in a multi-threaded manner, you are then of course using the underlying SSL library multi-threaded and those libs might have their own requirements on this issue. Basically, you need to provide one or two functions to allow it to function properly. For all details, see this:

    OpenSSL

    http://www.openssl.org/docs/crypto/threads.html#DESCRIPTION

    GnuTLS

    http://www.gnu.org/software/gnutls/manual/html_node/Multi_002dthreaded-applications.html

    NSS

    is claimed to be thread-safe already without anything required.

    yassl

    Required actions unknown.

    When using multiple threads you should set the CURLOPT_NOSIGNAL option to 1 for all handles. Everything will or might work fine except that timeouts are not honored during the DNS lookup - which you can work around by building libcurl with c-ares support. c-ares is a library that provides asynchronous name resolves. On some platforms, libcurl simply will not function properly multi-threaded unless this option is set.

    Also, note that CURLOPT_DNS_USE_GLOBAL_CACHE is not thread-safe.

Comments