1/***************************************************************************
2 *                                  _   _ ____  _
3 *  Project                     ___| | | |  _ \| |
4 *                             / __| | | | |_) | |
5 *                            | (__| |_| |  _ <| |___
6 *                             \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22/* A multi-threaded example that uses pthreads extensively to fetch
23 * X remote files at once */
24
25#include <stdio.h>
26#include <pthread.h>
27#include <curl/curl.h>
28
29#define NUMT 4
30
31/*
32  List of URLs to fetch.
33
34  If you intend to use a SSL-based protocol here you MUST setup the OpenSSL
35  callback functions as described here:
36
37  http://www.openssl.org/docs/crypto/threads.html#DESCRIPTION
38
39*/
40const char * const urls[NUMT]= {
41  "http://curl.haxx.se/",
42  "ftp://cool.haxx.se/",
43  "http://www.contactor.se/",
44  "www.haxx.se"
45};
46
47static void *pull_one_url(void *url)
48{
49  CURL *curl;
50
51  curl = curl_easy_init();
52  curl_easy_setopt(curl, CURLOPT_URL, url);
53  curl_easy_perform(curl); /* ignores error */
54  curl_easy_cleanup(curl);
55
56  return NULL;
57}
58
59
60/*
61   int pthread_create(pthread_t *new_thread_ID,
62   const pthread_attr_t *attr,
63   void * (*start_func)(void *), void *arg);
64*/
65
66int main(int argc, char **argv)
67{
68  pthread_t tid[NUMT];
69  int i;
70  int error;
71
72  /* Must initialize libcurl before any threads are started */
73  curl_global_init(CURL_GLOBAL_ALL);
74
75  for(i=0; i< NUMT; i++) {
76    error = pthread_create(&tid[i],
77                           NULL, /* default attributes please */
78                           pull_one_url,
79                           (void *)urls[i]);
80    if(0 != error)
81      fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);
82    else
83      fprintf(stderr, "Thread %d, gets %s\n", i, urls[i]);
84  }
85
86  /* now wait for all threads to terminate */
87  for(i=0; i< NUMT; i++) {
88    error = pthread_join(tid[i], NULL);
89    fprintf(stderr, "Thread %d terminated\n", i);
90  }
91
92  return 0;
93}
94