Web lists-archives.com

Re: GLib-CRITICAL Source ID 35 was not found when attempting to remove it




On Wed, 16 Sep 2015 10:54:15 +0530
Lokesh Chakka <lvenkatakumarchakka@xxxxxxxxx> wrote:
> markku,
> 
> i kept gtk_thread_enter / leave for experimental purpose.
> Actually issue was happening irrespective of existence of
> gtk_thread_enter / leave.

You are missing the point.  You must use gdk_threads_enter() and
gdk_threads_leave() both (i) where you already have them in main(), and
(ii) also around gtk_widget_queue_draw() in the callback.

However, gdk_threads_init(), gdk_threads_enter() and gdk_threads_leave()
are deprecated in GTK+3, and only work with X11 (not on windows).
Instead, you should do what has been suggested and send an event from
the worker thread to the GTK+ main loop using g_idle_add().  Then you
do not need to bother with gdk_threads_init(), gdk_threads_enter() or
gdk_threads_leave() in your program at all.  You will however need to
call g_thread_init() at the beginning of main() with glib < 2.32.
glib >= 2.32 is thread safe without g_thread_init() (which is a no-op).

Chris

> Thanks & Regards
> --
> Lokesh Chakka,
> Mobile: 9731023458
> 
> On Wed, Sep 16, 2015 at 10:21 AM, markku vire <markku.vire@xxxxxxxxx>
> wrote:
> 
> > Hi,
> >
> > Nowadays gdk_threads_enter ()/leave () are deprecated, so you
> > should not use them in newly written code. You need to do gtk calls
> > from main thread instead. You can send notifications to main thread
> > easily using g_idle_add, which schedules them to happen on next
> > main loop iteration.
> >
> > When working with older versions of gtk, you need to acquire gdk
> > lock before doing any gtk call from background thread. So, you
> > should move threads_enter/leave around queue_draw in your
> > thread_function.
> >
> > Hope this helps,
> >
> > -Markku-
> > 16.9.2015 7.03 "Lokesh Chakka" <lvenkatakumarchakka@xxxxxxxxx>
> > kirjoitti:
> >
> >> hello,
> >>
> >> I have written small piece of code and I am seeing thread_function
> >> is getting stuck. If I try to close the window, I am seeing GLib
> >> Critical error
> >>
> >> Can some body please help me fixing the issue ?
> >>
> >> following is the code:
> >>
> >> #include<gtk/gtk.h>
> >> #include<cairo.h>
> >> #include<stdlib.h>
> >> #include <pthread.h>
> >>
> >> GtkDrawingArea *darea;
> >> GtkVBox  *main_window_box;
> >> GtkWindow    *main_window;
> >>
> >> const int size = 500;
> >> int i;
> >>
> >> static void my_draw_event( GtkWidget *widget, cairo_t *cr, void
> >> *parameter )
> >> {
> >>     int a, b;
> >>     cairo_set_source_rgb( cr, 1, 0, 0 );
> >>     a = random()%size;
> >>     b = random()%size;
> >>     cairo_move_to( cr, a, a );
> >>     cairo_line_to( cr, b, b );
> >>     cairo_stroke(cr);
> >>     gtk_widget_show_all((GtkWidget*)main_window);
> >>     fprintf( stderr, "Values i: %4d a: %3d b: %3d\n", i, a, b );
> >> }
> >>
> >> static void *thread_function( void *parameter )
> >> {
> >>     const int seconds = 1000;
> >>
> >>     for( i=0; i<seconds; i++ )
> >>     {
> >>         gtk_widget_queue_draw( (GtkWidget*)darea );
> >>         sleep(1);
> >>     }
> >> }
> >>
> >> int main()
> >> {
> >>     pthread_t th;
> >>
> >>     gdk_threads_init();
> >>     gdk_threads_enter();
> >>     gtk_init( NULL, NULL );
> >>
> >>     main_window = (GtkWindow*)gtk_window_new(GTK_WINDOW_TOPLEVEL);
> >>
> >> g_signal_connect(G_OBJECT(main_window),"destroy",G_CALLBACK(gtk_main_quit),NULL);
> >>
> >>     main_window_box =
> >> (GtkVBox*)gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
> >> gtk_container_add (GTK_CONTAINER
> >> (main_window),(GtkWidget*)main_window_box);
> >>
> >>     darea = (GtkDrawingArea*)gtk_drawing_area_new();
> >>     gtk_box_pack_start( GTK_BOX( main_window_box ),
> >> (GtkWidget*)darea, TRUE, TRUE, 0 );
> >>     g_signal_connect( G_OBJECT(darea), "draw",
> >> G_CALLBACK( my_draw_event ), NULL );
> >>
> >>     gtk_window_set_position(GTK_WINDOW(main_window),
> >> GTK_WIN_POS_CENTER);
> >> gtk_window_set_default_size(GTK_WINDOW(main_window), size, size );
> >>
> >>     pthread_create( &th, NULL, thread_function, NULL );
> >>     gtk_widget_show_all((GtkWidget*)main_window);
> >>     gtk_main();
> >>     gdk_threads_leave();
> >>
> >>     return 0;
> >> }
> >>
> >> Thanks & Regards
> >> --
> >> Lokesh Chakka,
> >> Mobile: 9731023458
> >>
> >> _______________________________________________
> >> gtk-devel-list mailing list
> >> gtk-devel-list@xxxxxxxxx
> >> https://mail.gnome.org/mailman/listinfo/gtk-devel-list
> >>
> >>

_______________________________________________
gtk-devel-list mailing list
gtk-devel-list@xxxxxxxxx
https://mail.gnome.org/mailman/listinfo/gtk-devel-list