libcurl-thread.3 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. .\" generated by cd2nroff 0.1 from libcurl-thread.md
  2. .TH libcurl-thread 3 "2025-01-17" libcurl
  3. .SH NAME
  4. libcurl\-thread \- libcurl thread safety
  5. .SH Multi-threading with libcurl
  6. libcurl is thread safe but has no internal thread synchronization. You may have
  7. to provide your own locking should you meet any of the thread safety exceptions
  8. below.
  9. .SH Handles
  10. You must \fBnever\fP share the same handle in multiple threads. You can pass the
  11. handles around among threads, but you must never use a single handle from more
  12. than one thread at any given time.
  13. .SH Shared objects
  14. You can share certain data between multiple handles by using the share
  15. interface but you must provide your own locking and set
  16. \fIcurl_share_setopt(3)\fP CURLSHOPT_LOCKFUNC and CURLSHOPT_UNLOCKFUNC.
  17. Note that some items are specifically documented as not thread\-safe in the
  18. share API (the connection pool and HSTS cache for example).
  19. .SH TLS
  20. All current TLS libraries libcurl supports are thread\-safe.
  21. .IP OpenSSL
  22. OpenSSL 1.1.0+ can be safely used in multi\-threaded applications provided that
  23. support for the underlying OS threading API is built\-in. For older versions of
  24. OpenSSL, the user must set mutex callbacks.
  25. libcurl may not be able to fully clean up after multi\-threaded OpenSSL
  26. depending on how OpenSSL was built and loaded as a library. It is possible in
  27. some rare circumstances a memory leak could occur unless you implement your own
  28. OpenSSL thread cleanup.
  29. For example, on Windows if both libcurl and OpenSSL are linked statically to a
  30. DLL or application then OpenSSL may leak memory unless the DLL or application
  31. calls OPENSSL_thread_stop() before each thread terminates. If OpenSSL is built
  32. as a DLL then it does this cleanup automatically and there is no leak. If
  33. libcurl is built as a DLL and OpenSSL is linked statically to it then libcurl
  34. does this cleanup automatically and there is no leak (added in libcurl 8.8.0).
  35. Please review the OpenSSL documentation for a full list of circumstances:
  36. https://docs.openssl.org/3.0/man3/OPENSSL_init_crypto/#notes
  37. .SH Signals
  38. Signals are used for timing out name resolves (during DNS lookup) \- when built
  39. without using either the c\-ares or threaded resolver backends. On systems that
  40. have a signal concept.
  41. When using multiple threads you should set the \fICURLOPT_NOSIGNAL(3)\fP
  42. option to 1L for all handles. Everything works fine except that timeouts
  43. cannot be honored during DNS lookups \- which you can work around by building
  44. libcurl with c\-ares or threaded\-resolver support. c\-ares is a library that
  45. provides asynchronous name resolves. On some platforms, libcurl simply cannot
  46. function properly multi\-threaded unless the \fICURLOPT_NOSIGNAL(3)\fP option
  47. is set.
  48. When \fICURLOPT_NOSIGNAL(3)\fP is set to 1L, your application needs to deal
  49. with the risk of a SIGPIPE (that at least the OpenSSL backend can
  50. trigger). Note that setting \fICURLOPT_NOSIGNAL(3)\fP to 0L does not work in a
  51. threaded situation as there is a race condition where libcurl risks restoring
  52. the former signal handler while another thread should still ignore it.
  53. .SH Name resolving
  54. The \fBgethostbyname\fP or \fBgetaddrinfo\fP and other name resolving system
  55. calls used by libcurl are provided by your operating system and must be thread
  56. safe. It is important that libcurl can find and use thread safe versions of
  57. these and other system calls, as otherwise it cannot function fully thread
  58. safe. Some operating systems are known to have faulty thread
  59. implementations. We have previously received problem reports on *BSD (at least
  60. in the past, they may be working fine these days). Some operating systems that
  61. are known to have solid and working thread support are Linux, Solaris and
  62. Windows.
  63. .SH curl_global_* functions
  64. These functions are thread\-safe since libcurl 7.84.0 if
  65. \fIcurl_version_info(3)\fP has the \fBCURL_VERSION_THREADSAFE\fP feature bit
  66. set (most platforms).
  67. If these functions are not thread\-safe and you are using libcurl with multiple
  68. threads it is especially important that before use you call
  69. \fIcurl_global_init(3)\fP or \fIcurl_global_init_mem(3)\fP to explicitly
  70. initialize the library and its dependents, rather than rely on the "lazy"
  71. fail\-safe initialization that takes place the first time
  72. \fIcurl_easy_init(3)\fP is called. For an in\-depth explanation refer to
  73. \fIlibcurl(3)\fP section \fBGLOBAL CONSTANTS\fP.
  74. .SH Memory functions
  75. These functions, provided either by your operating system or your own
  76. replacements, must be thread safe. You can use \fIcurl_global_init_mem(3)\fP
  77. to set your own replacement memory functions.
  78. .SH Non-safe functions
  79. \fICURLOPT_DNS_USE_GLOBAL_CACHE(3)\fP is not thread\-safe.
  80. \fIcurl_version_info(3)\fP is not thread\-safe before libcurl initialization.
  81. .SH SEE ALSO
  82. .BR libcurl-security (3)