diff options
Diffstat (limited to 'docs')
-rw-r--r-- | docs/examples/smooth-gtk-thread.c | 70 |
1 files changed, 31 insertions, 39 deletions
diff --git a/docs/examples/smooth-gtk-thread.c b/docs/examples/smooth-gtk-thread.c index 81d405eaf..e149e5c0a 100644 --- a/docs/examples/smooth-gtk-thread.c +++ b/docs/examples/smooth-gtk-thread.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al. + * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -60,55 +60,47 @@ const char * const urls[]= { size_t write_file(void *ptr, size_t size, size_t nmemb, FILE *stream) { - /* printf("write_file\n"); */ return fwrite(ptr, size, nmemb, stream); } -/* https://weather.com/weather/today/l/46214?cc=*&dayf=5&unit=i */ -void *pull_one_url(void *NaN) +static void run_one(gchar *http, int j) { - /* Stop threads from entering unless j is incremented */ - pthread_mutex_lock(&lock); - while(j < num_urls) { - CURL *curl; - gchar *http; + FILE *outfile = fopen(urls[j], "wb"); + CURL *curl; + curl = curl_easy_init(); + if(curl) { printf("j = %d\n", j); - http = - g_strdup_printf("xoap.weather.com/weather/local/%s?cc=*&dayf=5&unit=i\n", - urls[j]); - - printf("http %s", http); - - curl = curl_easy_init(); - if(curl) { - - FILE *outfile = fopen(urls[j], "wb"); - - /* Set the URL and transfer type */ - curl_easy_setopt(curl, CURLOPT_URL, http); - - /* Write to the file */ - curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_file); - - j++; /* critical line */ - pthread_mutex_unlock(&lock); + /* Set the URL and transfer type */ + curl_easy_setopt(curl, CURLOPT_URL, http); - curl_easy_perform(curl); + /* Write to the file */ + curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_file); + curl_easy_perform(curl); - fclose(outfile); - printf("fclose\n"); + fclose(outfile); + curl_easy_cleanup(curl); + } +} - curl_easy_cleanup(curl); +void *pull_one_url(void *NaN) +{ + /* protect the reading and increasing of 'j' with a mutex */ + pthread_mutex_lock(&lock); + while(j < num_urls) { + int i = j; + j++; + pthread_mutex_unlock(&lock); + http = g_strdup_printf("https://example.com/%s", urls[i]); + if(http) { + run_one(http, i); + g_free(http); } - g_free(http); - - /* Adds more latency, testing the mutex.*/ - sleep(1); - - } /* end while */ + pthread_mutex_lock(&lock); + } + pthread_mutex_unlock(&lock); return NULL; } |