• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1---
2c: Copyright (C) Daniel Stenberg, <daniel.se>, et al.
3SPDX-License-Identifier: curl
4Title: libcurl-thread
5Section: 3
6Source: libcurl
7See-also:
8  - libcurl-security (3)
9---
10
11# NAME
12
13libcurl-thread - libcurl thread safety
14
15# Multi-threading with libcurl
16
17libcurl is thread safe but has no internal thread synchronization. You may have
18to provide your own locking should you meet any of the thread safety exceptions
19below.
20
21# Handles
22
23You must **never** share the same handle in multiple threads. You can pass the
24handles around among threads, but you must never use a single handle from more
25than one thread at any given time.
26
27# Shared objects
28
29You can share certain data between multiple handles by using the share
30interface but you must provide your own locking and set
31curl_share_setopt(3) CURLSHOPT_LOCKFUNC and CURLSHOPT_UNLOCKFUNC.
32
33Note that some items are specifically documented as not thread-safe in the
34share API (the connection pool and HSTS cache for example).
35
36# TLS
37
38All current TLS libraries libcurl supports are thread-safe. OpenSSL 1.1.0+ can
39be safely used in multi-threaded applications provided that support for the
40underlying OS threading API is built-in. For older versions of OpenSSL, the
41user must set mutex callbacks.
42
43# Signals
44
45Signals are used for timing out name resolves (during DNS lookup) - when built
46without using either the c-ares or threaded resolver backends. On systems that
47have a signal concept.
48
49When using multiple threads you should set the CURLOPT_NOSIGNAL(3)
50option to 1L for all handles. Everything works fine except that timeouts
51cannot be honored during DNS lookups - which you can work around by building
52libcurl with c-ares or threaded-resolver support. c-ares is a library that
53provides asynchronous name resolves. On some platforms, libcurl simply cannot
54function properly multi-threaded unless the CURLOPT_NOSIGNAL(3) option
55is set.
56
57When CURLOPT_NOSIGNAL(3) is set to 1L, your application needs to deal
58with the risk of a SIGPIPE (that at least the OpenSSL backend can
59trigger). Note that setting CURLOPT_NOSIGNAL(3) to 0L does not work in a
60threaded situation as there is a race condition where libcurl risks restoring
61the former signal handler while another thread should still ignore it.
62
63# Name resolving
64
65The **gethostbyname** or **getaddrinfo** and other name resolving system
66calls used by libcurl are provided by your operating system and must be thread
67safe. It is important that libcurl can find and use thread safe versions of
68these and other system calls, as otherwise it cannot function fully thread
69safe. Some operating systems are known to have faulty thread
70implementations. We have previously received problem reports on *BSD (at least
71in the past, they may be working fine these days). Some operating systems that
72are known to have solid and working thread support are Linux, Solaris and
73Windows.
74
75# curl_global_* functions
76
77These functions are thread-safe since libcurl 7.84.0 if
78curl_version_info(3) has the **CURL_VERSION_THREADSAFE** feature bit
79set (most platforms).
80
81If these functions are not thread-safe and you are using libcurl with multiple
82threads it is especially important that before use you call
83curl_global_init(3) or curl_global_init_mem(3) to explicitly
84initialize the library and its dependents, rather than rely on the "lazy"
85fail-safe initialization that takes place the first time
86curl_easy_init(3) is called. For an in-depth explanation refer to
87libcurl(3) section **GLOBAL CONSTANTS**.
88
89# Memory functions
90
91These functions, provided either by your operating system or your own
92replacements, must be thread safe. You can use curl_global_init_mem(3)
93to set your own replacement memory functions.
94
95# Non-safe functions
96
97CURLOPT_DNS_USE_GLOBAL_CACHE(3) is not thread-safe.
98
99curl_version_info(3) is not thread-safe before libcurl initialization.
100