Re: GTK Thread init problem
- From: James Scott Jr <skoona verizon net>
- To: prabahar k <informpraba gmail com>
- Cc: gtk-app-devel-list gnome org
- Subject: Re: GTK Thread init problem
- Date: Tue, 17 Apr 2007 00:02:22 -0400
On Mon, 2007-04-16 at 18:09 +0530, prabahar k wrote:
hi
i am trying to use a threaded GTK program. While running the
program it gives segmentation fault and it seem to be an
thread init problem. is there any thing i am missing? Please
give 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 as
gcc -Wall -g gtk19.c -o base -lpthread `pkg-config --cflags gtk+-2.0`
`pkg-config --libs gtk+-2.0` `gtk-config --cflags --libs gthread`
After core dump bt gives
#0 0x00484aa2 in pthread_mutex_lock () from /lib/libpthread.so.0
#1 0x0025f63a in g_mem_chunk_new () from /usr/lib/libglib-2.0.so.0
#2 0x002408c6 in g_ptr_array_sized_new () from /usr/lib/libglib-2.0.so.0
#3 0x002408e3 in g_ptr_array_new () from /usr/lib/libglib-2.0.so.0
#4 0x00146f0d in gdk_x11_atom_to_xatom () from /usr/lib/libgdk-x11-2.0.so.0
#5 0x00146f7b in gdk_atom_intern () from /usr/lib/libgdk-x11-2.0.so.0
#6 0x001454d9 in gdk_keymap_translate_keyboard_state () from
/usr/lib/libgdk-x11-2.0.so.0
#7 0x0011267e in gdk_pre_parse_libgtk_only () from /usr/lib/libgdk-
x11-2.0.so.0
#8 0x00598330 in gtk_disable_setlocale () from /usr/lib/libgtk-x11-2.0.so.0
#9 0x00264c8c in g_option_context_parse () from /usr/lib/libglib-2.0.so.0
#10 0x00598789 in gtk_parse_args () from /usr/lib/libgtk-x11-2.0.so.0
#11 0x005987c3 in gtk_init_check () from /usr/lib/libgtk-x11-2.0.so.0
#12 0x00598801 in gtk_init () from /usr/lib/libgtk-x11-2.0.so.0
#13 0x08048be7 in main (argc=2, argv=0xbfab7144) at gtk19.c:70
---
prabahar
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://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,
BEGIN CODE ***************************
/***********************gtk19m.c**************************/
/* gcc -O2 -Wall `pkg-config --cflags --libs gtk+-2.0 glib-2.0
gthread-2.0` gtk19m.c -o gtk19m */
#include <gtk/gtk.h>
#include <glib.h>
void destroy (GtkWidget *widget, gpointer data)
{
gtk_main_quit (); /* the window is almost invalid when this is
called */
/* consider "delete-event" */
}
/*
* Does not need gdk_threads_enter/leave() wrapping
* if no GTK/GDK apis are called
*/
gpointer argument_thread (gpointer *args)
{
for (;(gboolean)*args;) /* exit loop when flag is cleared */
{
/* sleep a while */
g_usleep(1000000);
g_print("Inside thread");
}
g_thread_exit (NULL); /* not required just good pratice */
return NULL;
}
int main (int argc, char *argv[])
{
GtkWidget *window;
GThread *gth = NULL; /* thread id */
gboolean b_run_flag = TRUE; /* used as exit flag for threads */
g_thread_init (NULL); /* Initialize GLIB thread support */
gdk_threads_init (); /* Initialize GDK locks */
gdk_threads_enter (); /* Acquire GDK locks */
gtk_init(&argc, &argv); /* initialize GTK */
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);
/* Create a bg thread using glib */
gth = g_thread_create ( (GThreadFunc)argument_thread,
(gpointer)&b_run_flag, TRUE, NULL);
gtk_main (); /* --- ANYTHING EXECUTED VIA THIS LOOP HAS GDK LOCKS
ALREADY APPLIED */
gdk_threads_leave (); /* release GDK locks */
b_run_flag = FALSE; /* flag threads to stop and exit */
g_thread_join(gth); /* wait for thread to exit */
return 0;
}
END CODE *****************************
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]