cURL / Mailing Lists / curl-library / Single Mail


Re: Is the callback specified in CURLOPT_WRITEFUNCTION thread-safe?

From: m brandenberg <>
Date: Fri, 4 Jul 2014 22:32:28 -0400 (EDT)

On Wed, 2 Jul 2014, Dan Fandrich wrote:
> > On Thu, Jul 03, 2014 at 12:12:34AM +0530, Ajay Garg wrote:
> >
> > You mean the "ptr" handle is different for each thread, in the callback
> > type:
> >
> >                  size_t write_callback(char *ptr, size_t
size, size_t nmemb,
> > void *userdata);
> ptr will point to a different internal libcurl buffer for each curl
handle, and
> since these handles are never shared between threads, yes, it will be
> in each thread.

I think that's a stronger guarantee than has ever been offered
by the API. (Hint is that it relies on assumptions about internal
buffers.) The 'ptr' value can come from handle-, connection-, or
request-lifetime allocations (probably other lifetimes). Once the
lifetime has expired, the buffer might be recycled and reallocated
to another thread. (And that's still an implementation detail.)

All you can say about 'ptr' is that the value won't be used by
another thread during the lifetime of a callback invocation. And
even that isn't guaranteed should the library want to inject a
piece of read-only boilerplate into the stream. Real answer:
don't make assumptions about 'ptr', it's dereferenceable if 'nmemb'
is non-zero, and that's about it.

Monty Brandenberg

List admin:
Received on 2014-07-05