cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: libcurl used in multiple components of an application

From: Jamie Lokier <jamie_at_shareable.org>
Date: Thu, 26 Mar 2009 12:35:57 +0000

Daniel Stenberg wrote:
> This is really simple: if you use more than one thread you MUST use
> curl_global_init(). Period. No discussion.
>
> Doing it differently will lead to sad faces and unhappiness at some point.
> Now, or later.

This reminds me.

On most platforms you know (or can configure) which threading
implementation is used with libcurl. In much the same way as you have
defaults for other things, when curl_global_init() isn't called by a
single-threaded app.

So why not something like this in libcurl itself (use the platform
equivalent of pthread_once_t):

    static pthread_once_t global_init_once = PTHREAD_ONCE_INIT;

    static void do_global_init(void)
    {
        curl_global_init(.../* The default flags */...);
    }

    static void do_nothing(void)
    {
    }

    curl_global_init(long flags)
    {
        pthread_once(&global_init_once, do_nothing);
        ... /* Init stuff. */
    }

    {
        /* Inside each API function. */
        pthread_once (&global_init_once, do_global_init);
        ... /* Do stuff. */
    }

Could that avoid those sad faces in some cases, and provide useful
defaults for most multi-threaded apps?

-- Jamie
Received on 2009-03-26