smooth-gtk-thread.c: enhance the mutex lock use
Reported-by: ryancaicse on github Fixes #7926 Closes #7931
This commit is contained in:
parent
9121032fe3
commit
f907faec79
@ -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]);
|
||||
/* Set the URL and transfer type */
|
||||
curl_easy_setopt(curl, CURLOPT_URL, http);
|
||||
|
||||
printf("http %s", http);
|
||||
/* Write to the file */
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_file);
|
||||
curl_easy_perform(curl);
|
||||
|
||||
curl = curl_easy_init();
|
||||
if(curl) {
|
||||
fclose(outfile);
|
||||
curl_easy_cleanup(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);
|
||||
|
||||
curl_easy_perform(curl);
|
||||
|
||||
fclose(outfile);
|
||||
printf("fclose\n");
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user