multithread.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 /* <DESC>
00023  * A multi-threaded example that uses pthreads to fetch several files at once
00024  * </DESC>
00025  */
00026 
00027 #include <stdio.h>
00028 #include <pthread.h>
00029 #include <curl/curl.h>
00030 
00031 #define NUMT 4
00032 
00033 /*
00034   List of URLs to fetch.
00035 
00036   If you intend to use a SSL-based protocol here you MUST setup the OpenSSL
00037   callback functions as described here:
00038 
00039   https://www.openssl.org/docs/crypto/threads.html#DESCRIPTION
00040 
00041 */
00042 const char * const urls[NUMT]= {
00043   "https://curl.haxx.se/",
00044   "ftp://cool.haxx.se/",
00045   "http://www.contactor.se/",
00046   "www.haxx.se"
00047 };
00048 
00049 static void *pull_one_url(void *url)
00050 {
00051   CURL *curl;
00052 
00053   curl = curl_easy_init();
00054   curl_easy_setopt(curl, CURLOPT_URL, url);
00055   curl_easy_perform(curl); /* ignores error */
00056   curl_easy_cleanup(curl);
00057 
00058   return NULL;
00059 }
00060 
00061 
00062 /*
00063    int pthread_create(pthread_t *new_thread_ID,
00064    const pthread_attr_t *attr,
00065    void * (*start_func)(void *), void *arg);
00066 */
00067 
00068 int main(int argc, char **argv)
00069 {
00070   pthread_t tid[NUMT];
00071   int i;
00072   int error;
00073 
00074   /* Must initialize libcurl before any threads are started */
00075   curl_global_init(CURL_GLOBAL_ALL);
00076 
00077   for(i=0; i< NUMT; i++) {
00078     error = pthread_create(&tid[i],
00079                            NULL, /* default attributes please */
00080                            pull_one_url,
00081                            (void *)urls[i]);
00082     if(0 != error)
00083       fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);
00084     else
00085       fprintf(stderr, "Thread %d, gets %s\n", i, urls[i]);
00086   }
00087 
00088   /* now wait for all threads to terminate */
00089   for(i=0; i< NUMT; i++) {
00090     error = pthread_join(tid[i], NULL);
00091     fprintf(stderr, "Thread %d terminated\n", i);
00092   }
00093 
00094   return 0;
00095 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:05