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
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2019-11-01 04:06:19 +08:00
|
|
|
* Copyright (C) 1998 - 2019, 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
|
2016-02-03 07:19:02 +08:00
|
|
|
* are also available at https://curl.haxx.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.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
2015-07-01 17:43:12 +08:00
|
|
|
/* <DESC>
|
|
|
|
* A multi-threaded example that uses pthreads to fetch several files at once
|
|
|
|
* </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
|
|
|
|
2017-02-09 23:16:21 +08:00
|
|
|
https://curl.haxx.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]= {
|
2016-02-03 07:19:02 +08:00
|
|
|
"https://curl.haxx.se/",
|
2001-05-04 17:35:43 +08:00
|
|
|
"ftp://cool.haxx.se/",
|
2018-09-23 17:20:26 +08:00
|
|
|
"https://www.cag.se/",
|
2001-05-04 17:35:43 +08:00
|
|
|
"www.haxx.se"
|
|
|
|
};
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|