2001-05-04 17:35:43 +08:00
|
|
|
/*****************************************************************************
|
2004-11-16 16:49:51 +08:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
2001-05-04 17:35:43 +08:00
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* A multi-threaded example that uses pthreads extensively to fetch
|
|
|
|
* X remote files at once */
|
|
|
|
|
|
|
|
#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.
|
|
|
|
|
|
|
|
If you intend to use a SSL-based protocol here you MUST setup the OpenSSL
|
|
|
|
callback functions as described here:
|
|
|
|
|
|
|
|
http://www.openssl.org/docs/crypto/threads.html#DESCRIPTION
|
|
|
|
|
|
|
|
*/
|
2008-04-04 04:28:32 +08:00
|
|
|
const char * const urls[NUMT]= {
|
2001-05-04 17:35:43 +08:00
|
|
|
"http://curl.haxx.se/",
|
|
|
|
"ftp://cool.haxx.se/",
|
|
|
|
"http://www.contactor.se/",
|
|
|
|
"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;
|
|
|
|
int error;
|
2008-04-04 04:28:32 +08:00
|
|
|
|
|
|
|
/* Must initialize libcurl before any threads are started */
|
|
|
|
curl_global_init(CURL_GLOBAL_ALL);
|
|
|
|
|
|
|
|
for(i=0; i< NUMT; i++) {
|
2001-05-04 17:35:43 +08:00
|
|
|
error = pthread_create(&tid[i],
|
|
|
|
NULL, /* default attributes please */
|
|
|
|
pull_one_url,
|
2007-07-13 05:11:10 +08:00
|
|
|
(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 */
|
2008-04-04 04:28:32 +08:00
|
|
|
for(i=0; i< NUMT; i++) {
|
2001-05-04 17:35:43 +08:00
|
|
|
error = pthread_join(tid[i], NULL);
|
|
|
|
fprintf(stderr, "Thread %d terminated\n", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|