Re: GTK Thread init problem



On Tue, 2007-04-17 at 13:56 +0530, prabahar k wrote:
Hi

Thanks Tristan for u r suggestion for using g_thread_create.
Thanks James for u r nice code.


Also If we want to retain the p_thread_create() in the program then
its enough to change the compile time flags to include
`pkg-config --cflags --libs gtk+-2.0 gthread-2.0`
Its working fine.

Here is the code:
/**************************gtk19mod.c************************
 compile:
 gcc -g -o gtk19mod gtk19mod.c `pkg-config --cflags --libs gtk+-2.0
gthread-2.0`
************************************************************/

#include <gtk/gtk.h>
#include <glib.h>
#include <pthread.h>
pthread_t thrd1;

void destroy (GtkWidget *widget, gpointer data)
{
 gtk_main_quit ();
}

void *argument_thread (void *args)
{


 for (;(*(gboolean *)args);)
   {
     /* sleep a while */
         g_usleep(1000000);
         g_print("Inside thread");
   }
 g_thread_exit (NULL);

OK, but don't mix the threading api's.  remove the g_thread_exit().  For
portability and stability - I always use glib's threading apis when
using GTK/GDK.  

James,



 return NULL;
}

int main (int argc, char *argv[])
{
 GtkWidget *window;

 gboolean  b_run_flag = TRUE;

 /* init threads */
 g_print("Going thread init\n");

 g_thread_init (NULL);
 gdk_threads_init ();

 gdk_threads_enter ();

 gtk_init(&argc, &argv);

 g_print("After init\n");
 /* create a window */

 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
 gtk_signal_connect (GTK_OBJECT (window), "destroy",
                     GTK_SIGNAL_FUNC (destroy), NULL);
 gtk_container_set_border_width (GTK_CONTAINER (window), 10);
 gtk_widget_show (window);


 pthread_create (&thrd1, NULL, argument_thread, (void*)&b_run_flag);


 gtk_main ();
 gdk_threads_leave ();

 b_run_flag = FALSE;
 return 0;
}
/************End*******************************************/


On 4/17/07, James Scott Jr <skoona verizon net> wrote:

On Mon, 2007-04-16 at 18:09 +0530, prabahar k wrote:

hi i am trying to use a threaded GTK program. While running theprogram it gives segmentation fault and it 
seem to be anthread init problem. is there any thing i am missing? Pleasegive u r comments.
the code is:/***********************gtk19.c**************************/#include <stdio.h>#include 
<stdlib.h>#include <unistd.h>#include <time.h>#include <gtk/gtk.h>#include <glib.h>#include <pthread.h>
pthread_t thrd1;
void destroy (GtkWidget *widget, gpointer data){ gtk_main_quit ();}
void *argument_thread (void *args){gdk_threads_enter (); for (;;)   {     /* sleep a while */


         sleep(1);         g_print("Inside thread");

   }gdk_threads_leave (); return NULL;}
int main (int argc, char *argv[]){ GtkWidget *window;
  /* init threads */
 g_thread_init (NULL); gdk_threads_init (); /* init gtk */ gdk_threads_enter ();/*[Line :70] */           
     gtk_init(&argc, &argv);
 g_print("After init\n");
 /* create a window */ window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
 gtk_signal_connect (GTK_OBJECT (window), "destroy",                     GTK_SIGNAL_FUNC (destroy), NULL);
 gtk_container_set_border_width (GTK_CONTAINER (window), 10); gtk_widget_show (window);
 pthread_create (&thrd1, NULL, argument_thread, NULL);
 gtk_main (); gdk_threads_leave ();
 return 0;}/**************************************************/
I compile it asgcc -Wall -g gtk19.c -o base -lpthread `pkg-config --cflags gtk+-2.0``pkg-config --libs 
gtk+-2.0` `gtk-config --cflags --libs gthread`

---
prabahar
_______________________________________________gtk-app-devel-list mailing listgtk-app-devel-list gnome 
orghttp://mail.gnome.org/mailman/listinfo/gtk-app-devel-list



prabahar,

I just modified your code to show the needed changes.  Notice that the
gdk_threads_enter/leave is removed from the thread and the thread is created
using glib's api.  I also updated the includes and compile command.

James,



_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]