Re: Problems with Threading Concept



Hi Naveen,

when it comes to multi-threading, I have almost no experience at all. If
I read the code right, you have the main thread which creates a
secondary thread which does nothing but print statements and calling
gtk_main_iteration.

I suspect that the delay is in the gtk_main_iteration. You could add
some print statements after that function call (I love debugging by
printf).

To be honest, I don't consider it wise to have gtk_main_iteration being
called in the second thread when gtk_main is called in the first thread.
That sounds like trouble.

If that does not help (quite likely), you should ask somebody with gtk
AND threading experience.


Regards,

Roland

naveen wrote:
> Hi all,
> 
> I have GUI developed based on gtk+-2.2.0,
> 
> 1) I have button to start some process (continuous).
> 2) I need to stop that process, when i click the same button.
> 
> to do the above tasks i developed a small application using threading
> concepts...
> 
> //////////////////////////////////////////////
> #include <gtk/gtk.h>
> #include "stdio.h"
> #include <pthread.h>
> 
>  int flag=1,toggle=0;
>     int i=0;
>     pthread_t yes_tid; 
> 
> void hello()
> {
> 
>     while (flag)
>     {
>   gdk_threads_enter ();
> 
>       printf ("%d %d\n",i++,flag);
>       if (gtk_events_pending())
>       gtk_main_iteration(); // Handle unprocessed GTK events
> 
>   gdk_threads_leave ();
>     }
> 
> }
> 
> void hello_print( GtkWidget *widget,
>             gpointer   data )
> {  
>    if(toggle==0)
>     {
>      toggle=1;
>      flag=1;
>      pthread_create (&yes_tid, NULL,(void *)hello, NULL);
>     }
>     else
>     {
>      flag=0;
>      toggle=0;
>     }
> 
> }
> int main(int   argc,
>           char *argv[] )
> {
>     
>     GtkWidget *window;
>     GtkWidget *button;
>     g_thread_init (NULL);
>     gdk_threads_init ();
> 
>   gdk_threads_enter ();
> 
>     gtk_init (&argc, &argv);
>     
>     /* create a new window */
>     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
>     
>     g_signal_connect (G_OBJECT (window), "destroy",
> 		      G_CALLBACK (gtk_main_quit), NULL);
>     
>     gtk_container_set_border_width (GTK_CONTAINER (window), 100);
>     
>     button = gtk_button_new_with_label("click it");
>     
>     g_signal_connect (G_OBJECT (button), "clicked",
> 		      G_CALLBACK (hello_print), NULL);
>     
>     gtk_container_add (GTK_CONTAINER (window), button);
>     
>     gtk_widget_show (button);
>     gtk_widget_show (window);
>     
>     gtk_main ();
> 
>    gdk_threads_leave ();
> 
>     return 0;
> }
> 
> //////////////////////////////////////////////////////
> But the problem here for me is, the stop is not happening immediatly
> after the click..
> 
> Is there any mistake the way i am doning it ... or do i need to follow
> any other procedure for it.. please help me out...
> 
> Regards,
> Naveen.
> 
> _______________________________________________
> gtkdatabox-list mailing list
> gtkdatabox-list gnome org
> http://mail.gnome.org/mailman/listinfo/gtkdatabox-list
> 




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