#include <stdio.h>
#include <pthread.h>
#include <curl/curl.h>

#define MY_URL "http://my-special-server/test.html"
//#define MY_URL "http://any-other-server/test.html"

/* this is my sub thread */
static void *sub_thread(void *dont_care) 
{ 
  CURL *curl = curl_easy_init();
  if (! curl) {
    printf("thread: curl_easy_init() failed\n");
    return 0;
  }
  curl_easy_setopt(curl, CURLOPT_VERBOSE,1);
  curl_easy_setopt(curl, CURLOPT_NOPROGRESS,1);
  curl_easy_setopt(curl, CURLOPT_NOSIGNAL,1);
  curl_easy_setopt(curl, CURLOPT_URL, MY_URL);
  /* this call fails for a certain server (but not for all!) if there wasn't 
     already a connection in the main thread (returns 7) */
  CURLcode res = curl_easy_perform(curl); 
  printf("thread: curl_easy_perform() returns %d\n", res);
  if (res)
    printf("thread: %s\n", curl_easy_strerror(res));
  curl_easy_cleanup(curl);

  return 0;
} 


int 
main (int argc, char *argv[]) 
{
  pthread_t thr;

  CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
  if (res) 
  {
    printf("main: curl_global_init() failed\n");
    return 1;
  }

  CURL *curl = curl_easy_init();
  if (! curl) {
    printf("main: curl_easy_init() failed\n");
    return 0;
  }
  curl_easy_setopt(curl, CURLOPT_VERBOSE,1);
  curl_easy_setopt(curl, CURLOPT_NOPROGRESS,1);
  curl_easy_setopt(curl, CURLOPT_NOSIGNAL,1);
  curl_easy_setopt(curl, CURLOPT_URL, MY_URL);
  // this call always works
  res = curl_easy_perform(curl); 
  printf("main: curl_easy_perform() returns %d\n", res);
  if (res)
    printf("main: %s\n", curl_easy_strerror(res));
 
  curl_easy_cleanup(curl);

  printf("main: starting thread\n");
  pthread_create(&thr, NULL, sub_thread, 0);
  pthread_join(thr, NULL);

  return 0;
}

