]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
smooth-gtk-thread.c: enhance the mutex lock use
authorDaniel Stenberg <daniel@haxx.se>
Sun, 31 Oct 2021 16:12:03 +0000 (17:12 +0100)
committerDaniel Stenberg <daniel@haxx.se>
Mon, 1 Nov 2021 07:42:40 +0000 (08:42 +0100)
Reported-by: ryancaicse on github
Fixes #7926
Closes #7931

docs/examples/smooth-gtk-thread.c

index 81d405eafa9bc0138f55e7cacb5948d248111ae9..e149e5c0a3f9275c06eb7f0e5abca14c201d24b7 100644 (file)
@@ -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;
 }