multithread.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * $Id$
  9. */
  10. /* A multi-threaded example that uses pthreads extensively to fetch
  11. * X remote files at once */
  12. #include <stdio.h>
  13. #include <pthread.h>
  14. #include <curl/curl.h>
  15. /* silly list of test-URLs */
  16. char *urls[]= {
  17. "http://curl.haxx.se/",
  18. "ftp://cool.haxx.se/",
  19. "http://www.contactor.se/",
  20. "www.haxx.se"
  21. };
  22. void *pull_one_url(void *url)
  23. {
  24. CURL *curl;
  25. curl = curl_easy_init();
  26. curl_easy_setopt(curl, CURLOPT_URL, url);
  27. curl_easy_perform(curl);
  28. curl_easy_cleanup(curl);
  29. return NULL;
  30. }
  31. /*
  32. int pthread_create(pthread_t *new_thread_ID,
  33. const pthread_attr_t *attr,
  34. void * (*start_func)(void *), void *arg);
  35. */
  36. int main(int argc, char **argv)
  37. {
  38. pthread_t tid[4];
  39. int i;
  40. int error;
  41. for(i=0; i< 4; i++) {
  42. error = pthread_create(&tid[i],
  43. NULL, /* default attributes please */
  44. pull_one_url,
  45. urls[i]);
  46. if(0 != error)
  47. fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);
  48. else
  49. fprintf(stderr, "Thread %d, gets %s\n", i, urls[i]);
  50. }
  51. /* now wait for all threads to terminate */
  52. for(i=0; i< 4; i++) {
  53. error = pthread_join(tid[i], NULL);
  54. fprintf(stderr, "Thread %d terminated\n", i);
  55. }
  56. return 0;
  57. }