quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

multithread.c (2662B)


      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) 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.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  * SPDX-License-Identifier: curl
     22  *
     23  ***************************************************************************/
     24 /* <DESC>
     25  * A multi-threaded program using pthreads to fetch several files at once
     26  * </DESC>
     27  */
     28 
     29 #include <stdio.h>
     30 #include <pthread.h>
     31 #include <curl/curl.h>
     32 
     33 #define NUMT 4
     34 
     35 /*
     36   List of URLs to fetch.
     37 
     38   If you intend to use an SSL-based protocol here you might need to setup TLS
     39   library mutex callbacks as described here:
     40 
     41   https://curl.se/libcurl/c/threadsafe.html
     42 
     43 */
     44 static const char * const urls[NUMT]= {
     45   "https://curl.se/",
     46   "ftp://example.com/",
     47   "https://example.net/",
     48   "www.example"
     49 };
     50 
     51 static void *pull_one_url(void *url)
     52 {
     53   CURL *curl;
     54 
     55   curl = curl_easy_init();
     56   curl_easy_setopt(curl, CURLOPT_URL, url);
     57   curl_easy_perform(curl); /* ignores error */
     58   curl_easy_cleanup(curl);
     59 
     60   return NULL;
     61 }
     62 
     63 
     64 /*
     65    int pthread_create(pthread_t *new_thread_ID,
     66    const pthread_attr_t *attr,
     67    void * (*start_func)(void *), void *arg);
     68 */
     69 
     70 int main(void)
     71 {
     72   pthread_t tid[NUMT];
     73   int i;
     74 
     75   /* Must initialize libcurl before any threads are started */
     76   curl_global_init(CURL_GLOBAL_ALL);
     77 
     78   for(i = 0; i < NUMT; i++) {
     79     int error = pthread_create(&tid[i],
     80                                NULL, /* default attributes please */
     81                                pull_one_url,
     82                                (void *)urls[i]);
     83     if(0 != error)
     84       fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);
     85     else
     86       fprintf(stderr, "Thread %d, gets %s\n", i, urls[i]);
     87   }
     88 
     89   /* now wait for all threads to terminate */
     90   for(i = 0; i < NUMT; i++) {
     91     pthread_join(tid[i], NULL);
     92     fprintf(stderr, "Thread %d terminated\n", i);
     93   }
     94   curl_global_cleanup();
     95   return 0;
     96 }