GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Jul 30, 2014 9:03 am

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: g_thread_new and GLOCK don't works
PostPosted: Mon May 13, 2013 11:16 am 
Offline
GTK+ Geek

Joined: Mon Mar 30, 2009 12:40 pm
Posts: 67
Location: España
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!!!!


Top
 Profile  
 
 Post subject: Re: g_thread_new and GLOCK don't works
PostPosted: Mon May 13, 2013 12:01 pm 
Offline
GTK+ Geek

Joined: Mon Mar 30, 2009 12:40 pm
Posts: 67
Location: España
It seems to work, but i don't undestand why:
Code:
#include <gtk/gtk.h>
#include <string.h>

G_LOCK_DEFINE(lock_thread);


typedef struct DATA {

GtkWidget *window,*grid,*progress,*label;
gchar lit[128];
GThread *thread_1,*thread_2,*thread_3;

} Data;



static gboolean update(Data *app)
{
 
//G_LOCK(lock_thread);
gtk_progress_bar_pulse(GTK_PROGRESS_BAR(app->progress));
gtk_label_set_text(GTK_LABEL(app->label),app->lit); 
printf("%s\n",app->lit);
//G_UNLOCK(lock_thread);
 
return FALSE;
}


static void *thread_1_func (Data *app)
{
  int i;

  for (i = 0; i < 100; i++)
    {
      g_usleep (100000); /* 0.1 s */
      strcpy(app->lit,"hilo1");
      gdk_threads_add_idle ((GSourceFunc)update,app);
     
    }

  return NULL;
}

static void *thread_2_func (Data *app)
{
  int i;

  for (i = 0; i < 100; i++)
    {
   
      g_usleep (200000);
      strcpy(app->lit,"     hilo2");
      gdk_threads_add_idle ((GSourceFunc)update,app);
    }

  return NULL;
}

static void *thread_3_func (Data *app)
{
  int i;

  for (i = 0; i < 100; i++)
    {
      g_usleep (300000);
      strcpy(app->lit,"          hilo3");
      gdk_threads_add_idle ((GSourceFunc)update,app);
    }

  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;
}


How gdk_threads_add_idle ((GSourceFunc)update,app); runs? And why this code works without G_LOCK?

Thanks!!!!


Top
 Profile  
 
 Post subject: Re: g_thread_new and GLOCK don't works
PostPosted: Mon May 13, 2013 1:33 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 734
Location: UK
Hi,

In your initial example you have created your own locking system which allows you to coordinate access to the GTK API from your three threads, but this does not coordinate access to the GTK API in the main thread. This is the reason for your errors. There is a way to to have multiple threads access GTK/GDK though this is now deprecated.

Your second example is correct and provides correct locking (automatically) by using gdk_threads_add_idle() for access to the GDK/GTK API. This way you do not need to have your own locking system.

_________________
E.


Top
 Profile  
 
 Post subject: Re: g_thread_new and GLOCK don't works
PostPosted: Tue May 14, 2013 5:17 am 
Offline
GTK+ Geek

Joined: Mon Mar 30, 2009 12:40 pm
Posts: 67
Location: España
OK. Understood. Thank you very much, Errol.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group