2011-03-10 18:48:02 +08:00
|
|
|
/***************************************************************************
|
2004-11-16 16:49:51 +08:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
2001-05-04 17:35:43 +08:00
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2023-01-02 20:51:48 +08:00
|
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
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
|
|
|
***************************************************************************/
|
2015-07-01 17:43:12 +08:00
|
|
|
/* <DESC>
|
2022-11-22 17:57:55 +08:00
|
|
|
* A multi-threaded program using pthreads to fetch several files at once
|
2015-07-01 17:43:12 +08:00
|
|
|
* </DESC>
|
|
|
|
*/
|
2001-05-04 17:35:43 +08:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
2008-04-04 04:28:32 +08:00
|
|
|
#define NUMT 4
|
|
|
|
|
2004-11-16 16:49:51 +08:00
|
|
|
/*
|
|
|
|
List of URLs to fetch.
|
|
|
|
|
2017-02-09 23:16:21 +08:00
|
|
|
If you intend to use a SSL-based protocol here you might need to setup TLS
|
|
|
|
library mutex callbacks as described here:
|
2004-11-16 16:49:51 +08:00
|
|
|
|
2020-11-04 21:02:01 +08:00
|
|
|
https://curl.se/libcurl/c/threadsafe.html
|
2004-11-16 16:49:51 +08:00
|
|
|
|
|
|
|
*/
|
2008-04-04 04:28:32 +08:00
|
|
|
const char * const urls[NUMT]= {
|
2020-11-04 21:02:01 +08:00
|
|
|
"https://curl.se/",
|
2021-08-30 15:51:07 +08:00
|
|
|
"ftp://example.com/",
|
|
|
|
"https://example.net/",
|
|
|
|
"www.example"
|
2001-05-04 17:35:43 +08:00
|
|
|
};
|
|
|
|
|
2008-04-04 04:28:32 +08:00
|
|
|
static void *pull_one_url(void *url)
|
2001-05-04 17:35:43 +08:00
|
|
|
{
|
|
|
|
CURL *curl;
|
|
|
|
|
|
|
|
curl = curl_easy_init();
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, url);
|
2004-11-16 16:49:51 +08:00
|
|
|
curl_easy_perform(curl); /* ignores error */
|
2001-05-04 17:35:43 +08:00
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-11-16 16:49:51 +08:00
|
|
|
/*
|
2001-05-04 17:35:43 +08:00
|
|
|
int pthread_create(pthread_t *new_thread_ID,
|
|
|
|
const pthread_attr_t *attr,
|
|
|
|
void * (*start_func)(void *), void *arg);
|
|
|
|
*/
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2008-04-04 04:28:32 +08:00
|
|
|
pthread_t tid[NUMT];
|
2001-05-04 17:35:43 +08:00
|
|
|
int i;
|
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]);
|
2001-05-04 17:35:43 +08:00
|
|
|
if(0 != error)
|
|
|
|
fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);
|
2004-11-16 16:49:51 +08:00
|
|
|
else
|
2001-05-04 17:35:43 +08:00
|
|
|
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);
|
2001-05-04 17:35:43 +08:00
|
|
|
fprintf(stderr, "Thread %d terminated\n", i);
|
|
|
|
}
|
2020-06-29 14:59:34 +08:00
|
|
|
curl_global_cleanup();
|
2001-05-04 17:35:43 +08:00
|
|
|
return 0;
|
|
|
|
}
|