cURL / Mailing Lists / curl-library / Single Mail


Re: Multi-thread initialization

From: Daniel Stenberg <>
Date: Mon, 14 Jan 2013 15:29:58 +0100 (CET)

On Mon, 14 Jan 2013, wrote:

> Based on your existing code, you use pthreads throughout the cURL code for
> ensuring thread-safety.

That's not exactly right. There's only one part of libcurl that uses pthreads:
the threaded resolver. And that is only conditionally built and not by
default. libcurl does not assume nor rely on any thread library by default...

libcurl itself is written to be fully thread-safe. A few external libraries
such as OpenSSL require their own mutex callbacks to be set to work properly
when used multi-threaded, but we don't set them within libcurl - which is a
frequent source for a bit of pain and problems to users.

> PolarSSL functions are thread-safe as long as the contexts are only used in
> a single thread at a time.

That sounds like the same restrictions the curl handles have. They must only
be used by one thread at a time - but libcurl itself does nothing to make sure
this is true but relies completely on that the application does the right

> The new random core uses a 'central' entropy pool. Thus we would like to
> create a static entropy_context inside polarssl.c.
> Calls to that context should be thread-locked to prevent race conditions.
> For that we need to initialize two mutexes for use in our mutexed entropy
> functions. Where can we put the pthread_mutex_init initialization calls
> inside cURL?

You'd then assume that the user of libcurl uses pthreads, which no default
libcurl code does. I'm not sure that's a problem, I'm mostly just stating
facts here...

> In our opinion the curl_global_init() function would be the best choice, but
> maybe a separated file or function call is desirable.

curl_global_init() is actually already documented as not thread-safe and it
must only be called from a single thread so you shouldn't need to protect it
with mutexes.

If you make sure the define called curlssl_init in lib/polarssl.h is updated
to call a suitable new function for this purpose within lib/polarssl.c it
shouldn't need much more changes than so I believe.

List admin:
Received on 2013-01-14