curl / Mailing Lists / curl-library / Single Mail


Multithreading with libcurl

From: Miloš Ljumović <>
Date: Thu, 15 Dec 2016 00:23:50 +0100

I wrote you before about my usage of multi stack among separate threads.
It works fine, although I am keep getting output like "forcibly told to
drain data" when using curl_multi_info_read.
If that's fine, good - but I suspect that, something might be wrong and
at some point app will crash.
Recently I had to introduce reading the http response using
write_callback which complicates my application even further, cause
entire logic with multi interface is asynchronous and after I added the
write_callback it required me introducing more structures and more
synchronization mechanisms (to properly determine for which of
concurrent requests write_callback was invoked).

I wanted to re-code it properly, if possible using the easy interface
cause I assume that unlike when using multi interface, write_callback
will be invoked (if http response exists) after calling curl_easy_perfom
but prior to function returns. In other words entire operation will be
synchronous. If so, few questions:

1. In as well as on it says I
should set up the openssl lock mechanisms. This bothers me:

for(i=0; i<CRYPTO_num_locks(); i++) {
     pthread_mutex_init(&(lockarray[i]), NULL);

What I do not understand is why openssl requires more than one mutex?
Isn't it one mutex (or CS) enough for synchronizing the threads, that
will access shared resource? Why more than one? Are there more shared
resources? Is it related to some openssl internal implementation?

2. Important for my application was that connections are re-used,
otherwise Apple Push Notification service would consider my requests (if
there are a lot of them) as DoS attacks. This is the reason I decided to
use "multi interface". Question is if I use easy interface, let's say
prepare everything on easy handle, call the curl_easy_perform then
curl_easy_reset, and then set up the handle again - in following call to
curl_easy_perform with the same domain ( but
different page will the same connection be used? If yes, easy interface
will do fine for me.

Miloš Ljumović
Operating systems specialist Spec.App.
List admin:
Received on 2016-12-15