K
K
Kirill Zhilyaev2018-07-21 12:47:38
C++ / C#
Kirill Zhilyaev, 2018-07-21 12:47:38

How safe is it to use CURL in multiple threads?

I have:

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 on a thread.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2018-07-21
@vt4a2h

Just takes off? Error code (by which you can get a message), any messages and debugging will help you.
Here is some example of multithreading: https://curl.haxx.se/libcurl/c/multithread.html . Perhaps you forgot to add something like: curl_global_init(CURL_GLOBAL_ALL); So there are no restrictions on multithreading.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question