Hello.
I'm try to make a program with three threads to update a progress bar and a label:
Code:
#include <gtk/gtk.h>
G_LOCK_DEFINE(lock_thread);
typedef struct DATA {
GtkWidget *window,*grid,*progress,*label;
GThread *thread_1,*thread_2,*thread_3;
} Data;
static void *thread_1_func (Data *app)
{
int i;
for (i = 0; i < 100; i++)
{
g_usleep (100000); /* 0.1 s */
G_LOCK(lock_thread);
gtk_progress_bar_pulse(GTK_PROGRESS_BAR(app->progress));
gtk_label_set_text(GTK_LABEL(app->label),"11111111111111111111111111111111");
printf("thread 1\n");
G_UNLOCK(lock_thread);
}
return NULL;
}
static void *thread_2_func (Data *app)
{
int i;
for (i = 0; i < 100; i++)
{
g_usleep (200000);
G_LOCK(lock_thread);
gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(app->progress),0);
gtk_label_set_text(GTK_LABEL(app->label)," 222222222222222222 ");
printf(" thread 2\n");
G_UNLOCK(lock_thread);
}
return NULL;
}
static void *thread_3_func (Data *app)
{
int i;
for (i = 0; i < 100; i++)
{
g_usleep (300000);
G_LOCK(lock_thread);
gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(app->progress),1);
gtk_label_set_text(GTK_LABEL(app->label),"33333333333333333333333333333333");
printf(" thread 3\n");
G_UNLOCK(lock_thread);
}
return NULL;
}
int
main (int argc,
char **argv)
{
Data *app = g_slice_new(Data);
gtk_init (&argc, &argv);
app->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size (GTK_WINDOW (app->window), 200, -1);
g_signal_connect(app->window, "destroy", gtk_main_quit, NULL);
app->grid = gtk_grid_new();
gtk_container_add (GTK_CONTAINER (app->window), app->grid);
app->progress = gtk_progress_bar_new();
gtk_grid_attach(GTK_GRID(app->grid),app->progress,0,0,1,1);
app->label = gtk_label_new("label");
gtk_grid_attach(GTK_GRID(app->grid),app->label,1,0,1,1);
gtk_widget_show_all (app->window);
app->thread_1 = g_thread_new ("one", (GThreadFunc)thread_1_func, app);
g_thread_unref(app->thread_1);
app->thread_2 = g_thread_new ("two", (GThreadFunc)thread_2_func, app);
g_thread_unref(app->thread_2);
app->thread_3 = g_thread_new ("tres", (GThreadFunc)thread_3_func, app);
g_thread_unref(app->thread_3);
gtk_main ();
g_slice_free(Data,app);
return 0;
}
When it runs, i have this error or something like this.
Code:
thread 1
thread 2
thread 1
thread 3
thread 1
thread 2
thread 1
thread 1
thread 3
thread 2
thread 1
(vdlerp:6837): Pango-CRITICAL **: pango_cairo_show_layout: assertion `PANGO_IS_LAYOUT (layout)' failed
thread 1
thread 2
thread 1
thread 3
(vdlerp:6837): Gtk-CRITICAL **: _gtk_css_computed_values_get_value: assertion `GTK_IS_CSS_COMPUTED_VALUES (values)' failed
(vdlerp:6837): Gtk-CRITICAL **: _gtk_css_computed_values_get_value: assertion `GTK_IS_CSS_COMPUTED_VALUES (values)' failed
(vdlerp:6837): Gtk-CRITICAL **: _gtk_css_computed_values_get_value: assertion `GTK_IS_CSS_COMPUTED_VALUES (values)' failed
(vdlerp:6837): Gtk-CRITICAL **: _gtk_css_computed_values_get_value: assertion `GTK_IS_CSS_COMPUTED_VALUES (values)' failed
Violación de segmento (access violation)
I don't understand why. G_LOCK Is not a mutex?. What is wrong?.
Thanks!!!!