libcurl-thread.3 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. .\" **************************************************************************
  2. .\" * _ _ ____ _
  3. .\" * Project ___| | | | _ \| |
  4. .\" * / __| | | | |_) | |
  5. .\" * | (__| |_| | _ <| |___
  6. .\" * \___|\___/|_| \_\_____|
  7. .\" *
  8. .\" * Copyright (C) 2015 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. .\" *
  10. .\" * This software is licensed as described in the file COPYING, which
  11. .\" * you should have received as part of this distribution. The terms
  12. .\" * are also available at https://curl.se/docs/copyright.html.
  13. .\" *
  14. .\" * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. .\" * copies of the Software, and permit persons to whom the Software is
  16. .\" * furnished to do so, under the terms of the COPYING file.
  17. .\" *
  18. .\" * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. .\" * KIND, either express or implied.
  20. .\" *
  21. .\" * SPDX-License-Identifier: curl
  22. .\" *
  23. .\" **************************************************************************
  24. .\"
  25. .TH libcurl-thread 3 "13 Jul 2015" "libcurl" "libcurl thread safety"
  26. .SH NAME
  27. libcurl-thread \- libcurl thread safety
  28. .SH "Multi-threading with libcurl"
  29. libcurl is thread safe but has no internal thread synchronization. You may have
  30. to provide your own locking should you meet any of the thread safety exceptions
  31. below.
  32. \fBHandles.\fP You must \fBnever\fP share the same handle in multiple threads.
  33. You can pass the handles around among threads, but you must never use a single
  34. handle from more than one thread at any given time.
  35. \fBShared objects.\fP You can share certain data between multiple handles by
  36. using the share interface but you must provide your own locking and set
  37. \fIcurl_share_setopt(3)\fP CURLSHOPT_LOCKFUNC and CURLSHOPT_UNLOCKFUNC.
  38. .SH TLS
  39. If you are accessing HTTPS or FTPS URLs in a multi-threaded manner, you are
  40. then of course using the underlying SSL library multi-threaded and those libs
  41. might have their own requirements on this issue. You may need to provide one
  42. or two functions to allow it to function properly:
  43. .IP OpenSSL
  44. OpenSSL 1.1.0+ "can be safely used in multi-threaded applications provided that
  45. support for the underlying OS threading API is built-in." In that case the
  46. engine is used by libcurl in a way that is fully thread-safe.
  47. https://www.openssl.org/docs/man1.1.0/man3/CRYPTO_THREAD_run_once.html#DESCRIPTION
  48. OpenSSL <= 1.0.2 the user must set callbacks.
  49. https://www.openssl.org/docs/man1.0.2/man3/CRYPTO_set_locking_callback.html#DESCRIPTION
  50. https://curl.se/libcurl/c/opensslthreadlock.html
  51. .IP GnuTLS
  52. https://gnutls.org/manual/html_node/Thread-safety.html
  53. .IP NSS
  54. thread-safe already without anything required.
  55. .IP Secure-Transport
  56. The engine is used by libcurl in a way that is fully thread-safe.
  57. .IP Schannel
  58. The engine is used by libcurl in a way that is fully thread-safe.
  59. .IP wolfSSL
  60. The engine is used by libcurl in a way that is fully thread-safe.
  61. .IP BoringSSL
  62. The engine is used by libcurl in a way that is fully thread-safe.
  63. .SH "Other areas of caution"
  64. .IP Signals
  65. Signals are used for timing out name resolves (during DNS lookup) - when built
  66. without using either the c-ares or threaded resolver backends. When using
  67. multiple threads you should set the \fICURLOPT_NOSIGNAL(3)\fP option to 1L for
  68. all handles. Everything will or might work fine except that timeouts are not
  69. honored during the DNS lookup - which you can work around by building libcurl
  70. with c-ares or threaded-resolver support. c-ares is a library that provides
  71. asynchronous name resolves. On some platforms, libcurl simply will not
  72. function properly multi-threaded unless the \fICURLOPT_NOSIGNAL(3)\fP option
  73. is set.
  74. When \fICURLOPT_NOSIGNAL(3)\fP is set to 1L, your application needs to deal
  75. with the risk of a SIGPIPE (that at least the OpenSSL backend can
  76. trigger). Note that setting \fICURLOPT_NOSIGNAL(3)\fP to 0L will not work in a
  77. threaded situation as there will be race where libcurl risks restoring the
  78. former signal handler while another thread should still ignore it.
  79. .IP "Name resolving"
  80. \fBgethostby* functions and other system calls.\fP These functions, provided
  81. by your operating system, must be thread safe. It is important that libcurl
  82. can find and use thread safe versions of these and other system calls, as
  83. otherwise it cannot function fully thread safe. Some operating systems are
  84. known to have faulty thread implementations. We have previously received
  85. problem reports on *BSD (at least in the past, they may be working fine these
  86. days). Some operating systems that are known to have solid and working thread
  87. support are Linux, Solaris and Windows.
  88. .IP "curl_global_* functions"
  89. These functions are thread-safe since libcurl 7.84.0 if
  90. \fIcurl_version_info(3)\fP has the \fBCURL_VERSION_THREADSAFE\fP feature bit
  91. set (most platforms).
  92. If these functions are not thread-safe and you are using libcurl with multiple
  93. threads it is especially important that before use you call
  94. \fIcurl_global_init(3)\fP or \fIcurl_global_init_mem(3)\fP to explicitly
  95. initialize the library and its dependents, rather than rely on the "lazy"
  96. fail-safe initialization that takes place the first time
  97. \fIcurl_easy_init(3)\fP is called. For an in-depth explanation refer to
  98. \fIlibcurl(3)\fP section \fBGLOBAL CONSTANTS\fP.
  99. .IP "Memory functions"
  100. These functions, provided either by your operating system or your own
  101. replacements, must be thread safe. You can use \fIcurl_global_init_mem(3)\fP
  102. to set your own replacement memory functions.
  103. .IP "Non-safe functions"
  104. \fICURLOPT_DNS_USE_GLOBAL_CACHE(3)\fP is not thread-safe.