summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorDaniel Stenberg <daniel@haxx.se>2021-10-31 17:12:03 +0100
committerDaniel Stenberg <daniel@haxx.se>2021-11-01 08:42:40 +0100
commitf907faec790f6bb5dc46102f1efa7e0faeef99ee (patch)
treebfa03dcf14fa10e2e2390f0a34875a4f456eecf8 /docs
parent9121032fe3ee8cbe811985f3d50b79d94edbeb7d (diff)
downloadcurl-f907faec790f6bb5dc46102f1efa7e0faeef99ee.tar.gz
smooth-gtk-thread.c: enhance the mutex lock use
Reported-by: ryancaicse on github Fixes #7926 Closes #7931
Diffstat (limited to 'docs')
-rw-r--r--docs/examples/smooth-gtk-thread.c70
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;
}