2011-03-10 18:48:02 +08:00
|
|
|
/***************************************************************************
|
2008-02-03 20:28:48 +08:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2023-01-02 20:51:48 +08:00
|
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2008-02-03 20:28:48 +08:00
|
|
|
*
|
2011-03-10 18:48:02 +08:00
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 21:02:01 +08:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2011-03-10 18:48:02 +08:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: curl
|
2022-05-17 17:16:50 +08:00
|
|
|
*
|
2011-03-10 18:48:02 +08:00
|
|
|
***************************************************************************/
|
2016-01-04 22:34:05 +08:00
|
|
|
/* <DESC>
|
|
|
|
* Show the required mutex callback setups for GnuTLS and OpenSSL when using
|
|
|
|
* libcurl multi-threaded.
|
|
|
|
* </DESC>
|
|
|
|
*/
|
2011-03-10 18:48:02 +08:00
|
|
|
/* A multi-threaded example that uses pthreads and fetches 4 remote files at
|
2024-06-02 02:41:41 +08:00
|
|
|
* once over HTTPS.
|
2008-02-03 20:28:48 +08:00
|
|
|
*
|
2024-06-02 02:41:41 +08:00
|
|
|
* Recent versions of OpenSSL and GnuTLS are thread safe by design, assuming
|
|
|
|
* support for the underlying OS threading API is built-in. Older revisions
|
|
|
|
* of this example demonstrated locking callbacks for the SSL library, which
|
|
|
|
* are no longer necessary. An older revision with callbacks can be found at
|
|
|
|
* https://github.com/curl/curl/blob/curl-7_88_1/docs/examples/threaded-ssl.c
|
2008-02-03 20:28:48 +08:00
|
|
|
*/
|
|
|
|
|
2008-02-24 07:00:24 +08:00
|
|
|
#define USE_OPENSSL /* or USE_GNUTLS accordingly */
|
|
|
|
|
2008-02-03 20:28:48 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
2008-04-04 04:28:32 +08:00
|
|
|
#define NUMT 4
|
|
|
|
|
2008-02-03 20:28:48 +08:00
|
|
|
/* List of URLs to fetch.*/
|
2008-04-04 04:28:32 +08:00
|
|
|
const char * const urls[]= {
|
2010-10-05 21:00:19 +08:00
|
|
|
"https://www.example.com/",
|
|
|
|
"https://www2.example.com/",
|
|
|
|
"https://www3.example.com/",
|
|
|
|
"https://www4.example.com/",
|
2008-02-03 20:28:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static void *pull_one_url(void *url)
|
|
|
|
{
|
|
|
|
CURL *curl;
|
|
|
|
|
|
|
|
curl = curl_easy_init();
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, url);
|
2021-10-31 23:34:44 +08:00
|
|
|
/* this example does not verify the server's certificate, which means we
|
2008-02-03 20:28:48 +08:00
|
|
|
might be downloading stuff from an impostor */
|
2008-05-23 05:20:07 +08:00
|
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
|
2008-02-03 20:28:48 +08:00
|
|
|
curl_easy_perform(curl); /* ignores error */
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2008-04-04 04:28:32 +08:00
|
|
|
pthread_t tid[NUMT];
|
2008-02-03 20:28:48 +08:00
|
|
|
int i;
|
2021-10-31 23:34:44 +08:00
|
|
|
(void)argc; /* we do not use any arguments in this example */
|
2008-02-03 20:28:48 +08:00
|
|
|
(void)argv;
|
|
|
|
|
2008-04-04 04:28:32 +08:00
|
|
|
/* Must initialize libcurl before any threads are started */
|
|
|
|
curl_global_init(CURL_GLOBAL_ALL);
|
|
|
|
|
2017-09-10 05:09:06 +08:00
|
|
|
for(i = 0; i< NUMT; i++) {
|
2019-05-20 17:50:23 +08:00
|
|
|
int error = pthread_create(&tid[i],
|
|
|
|
NULL, /* default attributes please */
|
|
|
|
pull_one_url,
|
|
|
|
(void *)urls[i]);
|
2008-02-03 20:28:48 +08:00
|
|
|
if(0 != error)
|
|
|
|
fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "Thread %d, gets %s\n", i, urls[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now wait for all threads to terminate */
|
2017-09-10 05:09:06 +08:00
|
|
|
for(i = 0; i< NUMT; i++) {
|
2019-05-20 17:50:23 +08:00
|
|
|
pthread_join(tid[i], NULL);
|
2008-02-03 20:28:48 +08:00
|
|
|
fprintf(stderr, "Thread %d terminated\n", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|