How safe is it to use CURL from multiple threads?

string CScanThread::HTTPReqest( string url )
 CURL *curl;
 CURLcode result;
 curl = curl_easy_init( );

 if ( curl )
 curl_easy_setopt( curl, CURLOPT_ERRORBUFFER, m_CURLErrorBuffer );
 curl_easy_setopt( curl, CURLOPT_URL, url );
 curl_easy_setopt( curl, CURLOPT_HEADER, 0 );
 curl_easy_setopt( curl, CURLOPT_CAINFO, "cacert.pem" );
 curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, writer );
 curl_easy_setopt( curl, CURLOPT_WRITEDATA, &m_ResponseBuffer );
 result = curl_easy_perform( curl );
 curl_easy_cleanup( curl );
 if ( result == CURLE_OK )
 return m_ResponseBuffer;


 return string ();


Rarely but the program crashes here:
result = curl_easy_perform( curl );
 curl_easy_cleanup( curl );

The class runs in a thread.
June 3rd 19 at 19:05
1 answer
June 3rd 19 at 19:07
Just crashes? Error code (which can be used and the message to), any communication and debug will help you.
Here is some example multithreaded use: . Perhaps you forgot to add something like this: curl_global_init(CURL_GLOBAL_ALL); So restrictions on multi-threading no.
Segmentation fault - Kelsi43 commented on June 3rd 19 at 19:10
I have a thread buttowski. I addition to the htyp request has more action - Kelsi43 commented on June 3rd 19 at 19:13
Buttowski flow under the hood will still be a pthread (system dependent of course).
Segfault can be for anything. Drive analyzers for errors with memory, try to reproduce the situation in the sandbox (small test project like in the example). - paris commented on June 3rd 19 at 19:16
so I pointed out the row. The query call on the cleaning. Appears randomly. - Kelsi43 commented on June 3rd 19 at 19:19
Great! And all would be nothing if you had the app only these lines and nothing could influence. If not, then the sandbox will be useful for the localization problem. In addition, you need to test on different data.
And in addition, it is unclear whether the input is valid url, what is m_ResponseBuffer, if he rummaged among the threads, is it secure, etc. The m_CURLErrorBuffer. If all is cleared between requests, whether enough memory. What writer, are there any side effects, etc.
I don't expect answers to these questions here, to me they are not too important. They are important to you to solve the problem. By the way, the approach is universal. - paris commented on June 3rd 19 at 19:22

Find more questions by tags C++