smooth-gtk-thread.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 /* <DESC>
00023  * A multi threaded application that uses a progress bar to show
00024  * status.  It uses Gtk+ to make a smooth pulse.
00025  * </DESC>
00026  */
00027 /*
00028  * Written by Jud Bishop after studying the other examples provided with
00029  * libcurl.
00030  *
00031  * To compile (on a single line):
00032  * gcc -ggdb `pkg-config --cflags  --libs gtk+-2.0` -lcurl -lssl -lcrypto
00033  *   -lgthread-2.0 -dl  smooth-gtk-thread.c -o smooth-gtk-thread
00034  */
00035 
00036 #include <stdio.h>
00037 #include <gtk/gtk.h>
00038 #include <glib.h>
00039 #include <unistd.h>
00040 #include <pthread.h>
00041 
00042 #include <curl/curl.h>
00043 
00044 #define NUMT 4
00045 
00046 pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
00047 int j = 0;
00048 gint num_urls = 9; /* Just make sure this is less than urls[]*/
00049 const char * const urls[]= {
00050   "90022",
00051   "90023",
00052   "90024",
00053   "90025",
00054   "90026",
00055   "90027",
00056   "90028",
00057   "90029",
00058   "90030"
00059 };
00060 
00061 size_t write_file(void *ptr, size_t size, size_t nmemb, FILE *stream)
00062 {
00063   /* printf("write_file\n"); */
00064   return fwrite(ptr, size, nmemb, stream);
00065 }
00066 
00067 /* http://xoap.weather.com/weather/local/46214?cc=*&dayf=5&unit=i */
00068 void *pull_one_url(void *NaN)
00069 {
00070   CURL *curl;
00071   CURLcode res;
00072   gchar *http;
00073   FILE *outfile;
00074 
00075   /* Stop threads from entering unless j is incremented */
00076   pthread_mutex_lock(&lock);
00077   while(j < num_urls) {
00078     printf("j = %d\n", j);
00079 
00080     http =
00081       g_strdup_printf("xoap.weather.com/weather/local/%s?cc=*&dayf=5&unit=i\n",
00082                       urls[j]);
00083 
00084     printf("http %s", http);
00085 
00086     curl = curl_easy_init();
00087     if(curl) {
00088 
00089       outfile = fopen(urls[j], "wb");
00090 
00091       /* Set the URL and transfer type */
00092       curl_easy_setopt(curl, CURLOPT_URL, http);
00093 
00094       /* Write to the file */
00095       curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
00096       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_file);
00097 
00098       j++;  /* critical line */
00099       pthread_mutex_unlock(&lock);
00100 
00101       res = curl_easy_perform(curl);
00102 
00103       fclose(outfile);
00104       printf("fclose\n");
00105 
00106       curl_easy_cleanup(curl);
00107     }
00108     g_free(http);
00109 
00110     /* Adds more latency, testing the mutex.*/
00111     sleep(1);
00112 
00113   } /* end while */
00114   return NULL;
00115 }
00116 
00117 
00118 gboolean pulse_bar(gpointer data)
00119 {
00120   gdk_threads_enter();
00121   gtk_progress_bar_pulse(GTK_PROGRESS_BAR (data));
00122   gdk_threads_leave();
00123 
00124   /* Return true so the function will be called again;
00125    * returning false removes this timeout function.
00126    */
00127   return TRUE;
00128 }
00129 
00130 void *create_thread(void *progress_bar)
00131 {
00132   pthread_t tid[NUMT];
00133   int i;
00134   int error;
00135 
00136   /* Make sure I don't create more threads than urls. */
00137   for(i=0; i < NUMT && i < num_urls ; i++) {
00138     error = pthread_create(&tid[i],
00139                            NULL, /* default attributes please */
00140                            pull_one_url,
00141                            NULL);
00142     if(0 != error)
00143       fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);
00144     else
00145       fprintf(stderr, "Thread %d, gets %s\n", i, urls[i]);
00146   }
00147 
00148   /* Wait for all threads to terminate. */
00149   for(i=0; i < NUMT && i < num_urls; i++) {
00150     error = pthread_join(tid[i], NULL);
00151     fprintf(stderr, "Thread %d terminated\n", i);
00152   }
00153 
00154   /* This stops the pulsing if you have it turned on in the progress bar
00155      section */
00156   g_source_remove(GPOINTER_TO_INT(g_object_get_data(G_OBJECT(progress_bar),
00157                                                     "pulse_id")));
00158 
00159   /* This destroys the progress bar */
00160   gtk_widget_destroy(progress_bar);
00161 
00162   /* [Un]Comment this out to kill the program rather than pushing close. */
00163   /* gtk_main_quit(); */
00164 
00165 
00166   return NULL;
00167 
00168 }
00169 
00170 static gboolean cb_delete(GtkWidget *window, gpointer data)
00171 {
00172   gtk_main_quit();
00173   return FALSE;
00174 }
00175 
00176 int main(int argc, char **argv)
00177 {
00178   GtkWidget *top_window, *outside_frame, *inside_frame, *progress_bar;
00179 
00180   /* Must initialize libcurl before any threads are started */
00181   curl_global_init(CURL_GLOBAL_ALL);
00182 
00183   /* Init thread */
00184   g_thread_init(NULL);
00185   gdk_threads_init();
00186   gdk_threads_enter();
00187 
00188   gtk_init(&argc, &argv);
00189 
00190   /* Base window */
00191   top_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
00192 
00193   /* Frame */
00194   outside_frame = gtk_frame_new(NULL);
00195   gtk_frame_set_shadow_type(GTK_FRAME(outside_frame), GTK_SHADOW_OUT);
00196   gtk_container_add(GTK_CONTAINER(top_window), outside_frame);
00197 
00198   /* Frame */
00199   inside_frame = gtk_frame_new(NULL);
00200   gtk_frame_set_shadow_type(GTK_FRAME(inside_frame), GTK_SHADOW_IN);
00201   gtk_container_set_border_width(GTK_CONTAINER(inside_frame), 5);
00202   gtk_container_add(GTK_CONTAINER(outside_frame), inside_frame);
00203 
00204   /* Progress bar */
00205   progress_bar = gtk_progress_bar_new();
00206   gtk_progress_bar_pulse(GTK_PROGRESS_BAR (progress_bar));
00207   /* Make uniform pulsing */
00208   gint pulse_ref = g_timeout_add(300, pulse_bar, progress_bar);
00209   g_object_set_data(G_OBJECT(progress_bar), "pulse_id",
00210                     GINT_TO_POINTER(pulse_ref));
00211   gtk_container_add(GTK_CONTAINER(inside_frame), progress_bar);
00212 
00213   gtk_widget_show_all(top_window);
00214   printf("gtk_widget_show_all\n");
00215 
00216   g_signal_connect(G_OBJECT (top_window), "delete-event",
00217                    G_CALLBACK(cb_delete), NULL);
00218 
00219   if(!g_thread_create(&create_thread, progress_bar, FALSE, NULL) != 0)
00220     g_warning("can't create the thread");
00221 
00222   gtk_main();
00223   gdk_threads_leave();
00224   printf("gdk_threads_leave\n");
00225 
00226   return 0;
00227 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:06