2011-03-10 05:48:02 -05:00
|
|
|
/***************************************************************************
|
2004-11-16 03:49:51 -05:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
2001-05-04 05:35:43 -04:00
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2017-02-09 10:16:21 -05:00
|
|
|
* Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-03-10 05:48:02 -05: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-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2011-03-10 05:48:02 -05: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 05:43:12 -04:00
|
|
|
/* <DESC>
|
|
|
|
* A multi-threaded example that uses pthreads to fetch several files at once
|
|
|
|
* </DESC>
|
|
|
|
*/
|
2001-05-04 05:35:43 -04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
2008-04-03 16:28:32 -04:00
|
|
|
#define NUMT 4
|
|
|
|
|
2004-11-16 03:49:51 -05:00
|
|
|
/*
|
|
|
|
List of URLs to fetch.
|
|
|
|
|
2017-02-09 10:16:21 -05: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 03:49:51 -05:00
|
|
|
|
2017-02-09 10:16:21 -05:00
|
|
|
https://curl.haxx.se/libcurl/c/threadsafe.html
|
2004-11-16 03:49:51 -05:00
|
|
|
|
|
|
|
*/
|
2008-04-03 16:28:32 -04:00
|
|
|
const char * const urls[NUMT]= {
|
2016-02-02 18:19:02 -05:00
|
|
|
"https://curl.haxx.se/",
|
2001-05-04 05:35:43 -04:00
|
|
|
"ftp://cool.haxx.se/",
|
|
|
|
"http://www.contactor.se/",
|
|
|
|
"www.haxx.se"
|
|
|
|
};
|
|
|
|
|
2008-04-03 16:28:32 -04:00
|
|
|
static void *pull_one_url(void *url)
|
2001-05-04 05:35:43 -04:00
|
|
|
{
|
|
|
|
CURL *curl;
|
|
|
|
|
|
|
|
curl = curl_easy_init();
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, url);
|
2004-11-16 03:49:51 -05:00
|
|
|
curl_easy_perform(curl); /* ignores error */
|
2001-05-04 05:35:43 -04:00
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-11-16 03:49:51 -05:00
|
|
|
/*
|
2001-05-04 05:35:43 -04: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-03 16:28:32 -04:00
|
|
|
pthread_t tid[NUMT];
|
2001-05-04 05:35:43 -04:00
|
|
|
int i;
|
|
|
|
int error;
|
2008-04-03 16:28:32 -04:00
|
|
|
|
|
|
|
/* Must initialize libcurl before any threads are started */
|
|
|
|
curl_global_init(CURL_GLOBAL_ALL);
|
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
for(i = 0; i< NUMT; i++) {
|
2001-05-04 05:35:43 -04:00
|
|
|
error = pthread_create(&tid[i],
|
|
|
|
NULL, /* default attributes please */
|
|
|
|
pull_one_url,
|
2007-07-12 17:11:10 -04:00
|
|
|
(void *)urls[i]);
|
2001-05-04 05:35:43 -04:00
|
|
|
if(0 != error)
|
|
|
|
fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);
|
2004-11-16 03:49:51 -05:00
|
|
|
else
|
2001-05-04 05:35:43 -04:00
|
|
|
fprintf(stderr, "Thread %d, gets %s\n", i, urls[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now wait for all threads to terminate */
|
2017-09-09 17:09:06 -04:00
|
|
|
for(i = 0; i< NUMT; i++) {
|
2001-05-04 05:35:43 -04:00
|
|
|
error = pthread_join(tid[i], NULL);
|
|
|
|
fprintf(stderr, "Thread %d terminated\n", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|