Annotation of embedaddon/curl/docs/examples/multithread.c, revision 1.1.1.1

1.1       misho       1: /***************************************************************************
                      2:  *                                  _   _ ____  _
                      3:  *  Project                     ___| | | |  _ \| |
                      4:  *                             / __| | | | |_) | |
                      5:  *                            | (__| |_| |  _ <| |___
                      6:  *                             \___|\___/|_| \_\_____|
                      7:  *
                      8:  * Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
                      9:  *
                     10:  * This software is licensed as described in the file COPYING, which
                     11:  * you should have received as part of this distribution. The terms
                     12:  * are also available at https://curl.haxx.se/docs/copyright.html.
                     13:  *
                     14:  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
                     15:  * copies of the Software, and permit persons to whom the Software is
                     16:  * furnished to do so, under the terms of the COPYING file.
                     17:  *
                     18:  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
                     19:  * KIND, either express or implied.
                     20:  *
                     21:  ***************************************************************************/
                     22: /* <DESC>
                     23:  * A multi-threaded example that uses pthreads to fetch several files at once
                     24:  * </DESC>
                     25:  */
                     26: 
                     27: #include <stdio.h>
                     28: #include <pthread.h>
                     29: #include <curl/curl.h>
                     30: 
                     31: #define NUMT 4
                     32: 
                     33: /*
                     34:   List of URLs to fetch.
                     35: 
                     36:   If you intend to use a SSL-based protocol here you might need to setup TLS
                     37:   library mutex callbacks as described here:
                     38: 
                     39:   https://curl.haxx.se/libcurl/c/threadsafe.html
                     40: 
                     41: */
                     42: const char * const urls[NUMT]= {
                     43:   "https://curl.haxx.se/",
                     44:   "ftp://cool.haxx.se/",
                     45:   "https://www.cag.se/",
                     46:   "www.haxx.se"
                     47: };
                     48: 
                     49: static void *pull_one_url(void *url)
                     50: {
                     51:   CURL *curl;
                     52: 
                     53:   curl = curl_easy_init();
                     54:   curl_easy_setopt(curl, CURLOPT_URL, url);
                     55:   curl_easy_perform(curl); /* ignores error */
                     56:   curl_easy_cleanup(curl);
                     57: 
                     58:   return NULL;
                     59: }
                     60: 
                     61: 
                     62: /*
                     63:    int pthread_create(pthread_t *new_thread_ID,
                     64:    const pthread_attr_t *attr,
                     65:    void * (*start_func)(void *), void *arg);
                     66: */
                     67: 
                     68: int main(int argc, char **argv)
                     69: {
                     70:   pthread_t tid[NUMT];
                     71:   int i;
                     72: 
                     73:   /* Must initialize libcurl before any threads are started */
                     74:   curl_global_init(CURL_GLOBAL_ALL);
                     75: 
                     76:   for(i = 0; i< NUMT; i++) {
                     77:     int error = pthread_create(&tid[i],
                     78:                                NULL, /* default attributes please */
                     79:                                pull_one_url,
                     80:                                (void *)urls[i]);
                     81:     if(0 != error)
                     82:       fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);
                     83:     else
                     84:       fprintf(stderr, "Thread %d, gets %s\n", i, urls[i]);
                     85:   }
                     86: 
                     87:   /* now wait for all threads to terminate */
                     88:   for(i = 0; i< NUMT; i++) {
                     89:     pthread_join(tid[i], NULL);
                     90:     fprintf(stderr, "Thread %d terminated\n", i);
                     91:   }
                     92: 
                     93:   return 0;
                     94: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>