[gtk+osx] =?iso-8859-1?Q?pb_with_thread_and_gtk-os0.7?=
Status: Beta
Brought to you by:
jralls
From: =?iso-8859-1?Q?<que...@li...> - 2004-07-16 13:01:00
|
Hi I am trying to do a native GTK project with multithread and gtk-os0.7.= But I think there are errors =0D=0Ain the g_thread_init function.=0D=0A=0D= =0AFor example : =0D=0A1) I make an empty project with Xcode (as prog.app= ) and I build it.=0D=0A2) I type the following below code (testthread.c).= (it comes from http://mail.gnome.org/archives/=0D=0Agtk-list/1999-Octobe= r/msg00412.html )=0D=0A3) I compile with :=0D=0A=0D=0Agcc -o prog testthr= ead.c `gtk-config --cflags --libs gthread` =0D=0A=0D=0A( where gtk-conf= ig is the gtk-config coming from http://gtk-osx.sourceforge.net/docs/gtk-= config)=0D=0A4) I move the new program into the prog.app with :=0D=0A=0D=0A= mv prog prog.app/Contents/MacOs/prog =0D=0A=0D=0A5) I start the program w= ith :=0D=0A=0D=0Aopen prog.app=0D=0A=0D=0A or by typing :=0D=0A=0D=0A./pr= og.app/Contents/MacOs/prog=0D=0A=0D=0AThe results are :=0D=0A1) no error = during the compilation=0D=0A2) the program is launching (a jumping applic= ation icon appears in the dock)=0D=0A3) I have no Apple menu and I can no= t click on the program. No events, no reactions at all ! Like if =0D=0Ath= e MacOs and/or GTK does not receive the events. And the icon is always ju= mping.=0D=0A4) If I comment the following line : =0D=0A=0D=0Ag_thread_ini= t(NULL);=0D=0A=0D=0AThe application is OK (I can click on the program, th= e buttons appears), but it is not multithread =0D=0A(only if an event app= ears like moving the window !!)=0D=0A=0D=0Atestthread.c :=0D=0A----------= --------=0D=0A=0D=0A#include <stdio.h>=0D=0A#include <stdlib.h>=0D=0A#inc= lude <unistd.h>=0D=0A#include <time.h>=0D=0A#include <gtk/gtk.h>=0D=0A#in= clude <glib.h>=0D=0A#include <pthread.h>=0D=0A=0D=0A#define YES_IT_IS = (1)=0D=0A#define NO_IT_IS_NOT (0)=0D=0A=0D=0Atypedef struct =0D=0A{=0D=0A= GtkWidget *label;=0D=0A int what;=0D=0A} yes_or_no_args;=0D=0A=0D=0Ast= atic volatile int yes_or_no =3D YES_IT_IS;=0D=0A=0D=0Agint delete_event(G= tkWidget *widget, GdkEvent *event, gpointer data)=0D=0A{=0D=0A return(FA= LSE);=0D=0A}=0D=0A=0D=0Avoid destroy(GtkWidget *widget, gpointer data)=0D= =0A{=0D=0A gtk_main_quit();=0D=0A}=0D=0A=0D=0Avoid *argument_thread(void= *args)=0D=0A{=0D=0A yes_or_no_args *data =3D (yes_or_no_args *)args;=0D= =0A=0D=0A for(;;)=0D=0A {=0D=0A /* sleep a while */=0D=0A s= leep(rand() / (RAND_MAX / 3) + 1);=0D=0A=0D=0A /* do we have to say = something? */=0D=0A if(yes_or_no !=3D data->what)=0D=0A {=0D=0A= /* set the variable */=0D=0A yes_or_no =3D data->what;= =0D=0A=0D=0A /* get GTK thread lock */=0D=0A gdk_thread= s_enter();=0D=0A=0D=0A /* set label text */=0D=0A if(da= ta->what =3D=3D YES_IT_IS)=0D=0A gtk_label_set_text(GTK_LABEL(= data->label), "O yes, it is!");=0D=0A else=0D=0A gtk_= label_set_text(GTK_LABEL(data->label), "O no, it isn't!");=0D=0A=0D=0A = /* release GTK thread lock */=0D=0A gdk_threads_leave();=0D= =0A }=0D=0A }=0D=0A=0D=0A return(NULL);=0D=0A}=0D=0A=0D=0Aint = main(int argc, char *argv[])=0D=0A{=0D=0A GtkWidget *window;=0D=0A GtkW= idget *label;=0D=0A yes_or_no_args yes_args, no_args;=0D=0A pthread_t n= o_tid, yes_tid;=0D=0A=0D=0A /* init threads */=0D=0A g_thread_init(NULL= );=0D=0A=0D=0A /* init gtk */=0D=0A gtk_init(&argc, &argv);=0D=0A=0D=0A= /* init random number generator */=0D=0A srand((unsigned int)time(NULL= ));=0D=0A=0D=0A /* create a window */=0D=0A window =3D gtk_window_new(G= TK_WINDOW_TOPLEVEL);=0D=0A =0D=0A gtk_signal_connect(GTK_OBJECT (window= ), "delete_event",=0D=0A GTK_SIGNAL_FUNC(delete_event= ), NULL);=0D=0A =0D=0A gtk_signal_connect(GTK_OBJECT (window), = "destroy",=0D=0A GTK_SIGNAL_FUNC(destroy), NULL);=0D=0A= =0D=0A#if (GTK_MAJOR_VERSION =3D=3D 1) && (GTK_MINOR_VERSION =3D=3D 0)=0D= =0A gtk_container_border_width(GTK_CONTAINER (window), 10);=0D=0A#else = =0D=0A gtk_container_set_border_width(GTK_CONTAINER (window), 10);=0D=0A= #endif=0D=0A=0D=0A /* create a label */=0D=0A label =3D gtk_label_new("= And now for something completely different ...");=0D=0A gtk_container_ad= d(GTK_CONTAINER(window), label);=0D=0A =0D=0A /* show everything */=0D=0A= gtk_widget_show(label);=0D=0A gtk_widget_show (window);=0D=0A=0D=0A /= * create the threads */=0D=0A yes_args.label =3D label;=0D=0A yes_args.= what =3D YES_IT_IS;=0D=0A pthread_create(&yes_tid, NULL, argument_thread= , &yes_args);=0D=0A=0D=0A no_args.label =3D label;=0D=0A no_args.what =3D= NO_IT_IS_NOT;=0D=0A pthread_create(&no_tid, NULL, argument_thread, &no_= args);=0D=0A=0D=0A /* enter the GTK main loop */=0D=0A gdk_threads_ente= r();=0D=0A gtk_main();=0D=0A gdk_threads_leave();=0D=0A=0D=0A return(0= );=0D=0A}=0D=0A=0D=0AMerci,=0D=0AQuentin Quadrat=0A=0A*******************= ***** ADSL ILLIMITE TISCALI + TELEPHONE GRATUIT ************************ = =0ASurfez 40 fois plus vite pour 30EUR/mois seulement ! Et t=E9l=E9phone= z partout en France gratuitement, =0Avers les postes fixes (hors num=E9r= os sp=E9ciaux). Tarifs tr=E8s avantageux vers les mobiles et l'internatio= nal !=0APour profiter de cette offre exceptionnelle, cliquez ici : http:/= /register.tiscali.fr/adsl (voir conditions sur le site)=0A |