1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
22 /* A multi-threaded example that uses pthreads and fetches 4 remote files at
23 * once over HTTPS. The lock callbacks and stuff assume OpenSSL or GnuTLS
26 * OpenSSL docs for this:
27 * http://www.openssl.org/docs/crypto/threads.html
28 * gcrypt docs for this:
29 * http://gnupg.org/documentation/manuals/gcrypt/Multi_002dThreading.html
32 #define USE_OPENSSL /* or USE_GNUTLS accordingly */
36 #include <curl/curl.h>
40 /* we have this global to let the callback get easy access to it */
41 static pthread_mutex_t *lockarray;
44 #include <openssl/crypto.h>
45 static void lock_callback(int mode, int type, char *file, int line)
49 if (mode & CRYPTO_LOCK) {
50 pthread_mutex_lock(&(lockarray[type]));
53 pthread_mutex_unlock(&(lockarray[type]));
57 static unsigned long thread_id(void)
61 ret=(unsigned long)pthread_self();
65 static void init_locks(void)
69 lockarray=(pthread_mutex_t *)OPENSSL_malloc(CRYPTO_num_locks() *
70 sizeof(pthread_mutex_t));
71 for (i=0; i<CRYPTO_num_locks(); i++) {
72 pthread_mutex_init(&(lockarray[i]),NULL);
75 CRYPTO_set_id_callback((unsigned long (*)())thread_id);
76 CRYPTO_set_locking_callback((void (*)())lock_callback);
79 static void kill_locks(void)
83 CRYPTO_set_locking_callback(NULL);
84 for (i=0; i<CRYPTO_num_locks(); i++)
85 pthread_mutex_destroy(&(lockarray[i]));
87 OPENSSL_free(lockarray);
95 GCRY_THREAD_OPTION_PTHREAD_IMPL;
99 gcry_control(GCRYCTL_SET_THREAD_CBS);
105 /* List of URLs to fetch.*/
106 const char * const urls[]= {
107 "https://www.example.com/",
108 "https://www2.example.com/",
109 "https://www3.example.com/",
110 "https://www4.example.com/",
113 static void *pull_one_url(void *url)
117 curl = curl_easy_init();
118 curl_easy_setopt(curl, CURLOPT_URL, url);
119 /* this example doesn't verify the server's certificate, which means we
120 might be downloading stuff from an impostor */
121 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
122 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
123 curl_easy_perform(curl); /* ignores error */
124 curl_easy_cleanup(curl);
129 int main(int argc, char **argv)
134 (void)argc; /* we don't use any arguments in this example */
137 /* Must initialize libcurl before any threads are started */
138 curl_global_init(CURL_GLOBAL_ALL);
142 for(i=0; i< NUMT; i++) {
143 error = pthread_create(&tid[i],
144 NULL, /* default attributes please */
148 fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);
150 fprintf(stderr, "Thread %d, gets %s\n", i, urls[i]);
153 /* now wait for all threads to terminate */
154 for(i=0; i< NUMT; i++) {
155 error = pthread_join(tid[i], NULL);
156 fprintf(stderr, "Thread %d terminated\n", i);