blob: 5f59a99d7bd73875cfaad7d9db84689be4d4f169 [file] [log] [blame]
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001/*****************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 */
9
10/* A multi-threaded example that uses pthreads extensively to fetch
11 * X remote files at once */
12
13#include <stdio.h>
14#include <pthread.h>
15#include <curl/curl.h>
16
17#define NUMT 4
18
19/*
20 List of URLs to fetch.
21
22 If you intend to use a SSL-based protocol here you MUST setup the OpenSSL
23 callback functions as described here:
24
25 http://www.openssl.org/docs/crypto/threads.html#DESCRIPTION
26
27*/
28const char * const urls[NUMT]= {
29 "http://curl.haxx.se/",
30 "ftp://cool.haxx.se/",
31 "http://www.contactor.se/",
32 "www.haxx.se"
33};
34
35static void *pull_one_url(void *url)
36{
37 CURL *curl;
38
39 curl = curl_easy_init();
40 curl_easy_setopt(curl, CURLOPT_URL, url);
41 curl_easy_perform(curl); /* ignores error */
42 curl_easy_cleanup(curl);
43
44 return NULL;
45}
46
47
48/*
49 int pthread_create(pthread_t *new_thread_ID,
50 const pthread_attr_t *attr,
51 void * (*start_func)(void *), void *arg);
52*/
53
54int main(int argc, char **argv)
55{
56 pthread_t tid[NUMT];
57 int i;
58 int error;
59
60 /* Must initialize libcurl before any threads are started */
61 curl_global_init(CURL_GLOBAL_ALL);
62
63 for(i=0; i< NUMT; i++) {
64 error = pthread_create(&tid[i],
65 NULL, /* default attributes please */
66 pull_one_url,
67 (void *)urls[i]);
68 if(0 != error)
69 fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);
70 else
71 fprintf(stderr, "Thread %d, gets %s\n", i, urls[i]);
72 }
73
74 /* now wait for all threads to terminate */
75 for(i=0; i< NUMT; i++) {
76 error = pthread_join(tid[i], NULL);
77 fprintf(stderr, "Thread %d terminated\n", i);
78 }
79
80 return 0;
81}