cURL / Mailing Lists / curl-library / Single Mail

curl-library

curl library bug

From: Viktor Molnar <viktor.molnar_at_inbox.com>
Date: Wed, 23 Jun 2010 02:08:39 -0800
I have been trying this example:
 http://curl.haxx.se/libcurl/c/multithread.html
and it's working fine.

But, when I made a few changes (num of threads and URL), it's
generated an 'segmentation fault'.

I had same problem on:
 - 2.6.28-19-generic #61-Ubuntu  |  curl 7.18.2
 - 2.6.34-ARCH #1 SMP  |  curl 7.20.1

-----------

Here is my code:

#include <stdio.h>
#include <pthread.h>
#include <curl/curl.h>
 
#define NUMT 10
 
/*
  List of URLs to fetch.
 
  If you intend to use a SSL-based protocol here you MUST setup the OpenSSL
  callback functions as described here:
 
  http://www.openssl.org/docs/crypto/threads.html#DESCRIPTION
 
*/
const char * const urls[NUMT]= {
  "http://www.alza.sk",
  "http://www.alza.sk",
  "http://www.alza.sk",
  "http://www.alza.sk",
  "http://www.alza.sk",
  "http://www.alza.sk",
  "http://www.alza.sk",
  "http://www.alza.sk",
  "http://www.alza.sk",
  "http://www.alza.sk"
};
 
static void *pull_one_url(void *url)
{
  CURL *curl;
 
  curl = curl_easy_init();
  curl_easy_setopt(curl, CURLOPT_URL, url);
  curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1);
  curl_easy_perform(curl); /* ignores error */
  curl_easy_cleanup(curl);
 
  return NULL;
}
 
 
/*
   int pthread_create(pthread_t *new_thread_ID,
   const pthread_attr_t *attr,
   void * (*start_func)(void *), void *arg);
*/
 
int main(int argc, char **argv)
{
  pthread_t tid[NUMT];
  int i;
  int error;
 
  /* Must initialize libcurl before any threads are started */
  curl_global_init(CURL_GLOBAL_ALL);
 
  for(i=0; i< NUMT; i++) {
    error = pthread_create(&tid[i],
                           NULL, /* default attributes please */
                           pull_one_url,
                           (void *)urls[i]);
    if(0 != error)
      fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);
    else
      fprintf(stderr, "Thread %d, gets %s\n", i, urls[i]);
  }
 
  /* now wait for all threads to terminate */
  for(i=0; i< NUMT; i++) {
    error = pthread_join(tid[i], NULL);
    fprintf(stderr, "Thread %d terminated\n", i);
  }
 
  return 0;
}



Try IM ToolPack Send your photos by email in seconds...
Try FREE IM ToolPack at www.imtoolpack.com
Works in all emails, instant messengers, blogs, forums and social networks.

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2010-06-23