Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

What is the usage of "use Gtk2 -init -threads-init"?

by llancet (Friar)
on Nov 02, 2009 at 02:52 UTC ( #804402=perlquestion: print w/replies, xml ) Need Help??
llancet has asked for the wisdom of the Perl Monks concerning the following question:

In gnome's site, it is said that threads must be created before any GTK widgets created, just like those on Tk. However, in Gtk2 perldoc, it provides an option:

use Gtk2 qw/-init -threads-init/;
, and says that would allow the use of
Gtk2::Gdk::Threads->enter
and
Gtk2::Gdk::Threads->leave

I cannot find any more documentation about that. Does that kind of mechanism provides threaded callbacks?

Replies are listed 'Best First'.
Re: What is the usage of "use Gtk2 -init -threads-init"?
by zentara (Archbishop) on Nov 02, 2009 at 13:04 UTC
    From the gtk2-perl maillist ( the best place for deep questions):

    # wise words of muppet ############################## That example predates some other important developments in threading support... You want to have: use Gtk2 '-init', '-threads-init'; The -threads-init import option is equalivent to calling Gtk2::Gdk::Threads->init(). This makes the calls to Gtk2::Gdk::Threads->enter() and Gtk2::Gdk::Threads->leave() actual +ly have effect for wrapping calls to gui functions from child threads +; see the C reference docs for gdk_threads_enter() and gdk_threads_l +eave (). You'll also want to call Glib::Object->set_threadsafe (TRUE); befo +re doing much of anything; this enables object tracking within the bindings, to avoid the "object destroyed out from under you" problem. The bindings must be built with thread support, which is + the default. Note, however, that i have not personally played with this thread- + safety stuff to any degree, so YMMV. #####################################
    back to me

    .... from what i recall in my testing, the "init" stuff, is more intended for ref counts and object safety across threads

    ... whearas the "enter" "leave" stuff, is an attempt at thread safety as the program runs, where you are indicating that you intended to enter the thread, and manipulate it only..... this is problem prone...and complex, like the main thread must be entered and left also

    .... the general advice from the c gurus is to use glib idle_add, which informs the mainloop to only run that code at the next convenient time..... the code can be dynamically named external subs..

    an example

    #!/usr/bin/perl use warnings; use strict; use Glib; use Glib qw/TRUE FALSE/; use threads; use threads::shared; my $count = 0; my $thread; my $die:shared = 0; my $main_loop = Glib::MainLoop->new; #test timer my $timer = Glib::Timeout->add (1000, \&timer_callback, undef, 1 ); #timer to start thread after 4 seconds my $timer1 = Glib::Timeout->add (4000, \&timer1_callback, undef, 1 ); #timer to stop thread after 8 seconds my $timer2 = Glib::Timeout->add (8000, \&timer2_callback, undef, 1 ); $main_loop->run; sub timer_callback{ $count++; print "$count\n"; return 1; } sub timer1_callback{ $thread = threads->new(\&work); return 0; #run once only } sub timer2_callback{ $die = 1; $thread->join; return 0; #run once only } sub work{ $|++; while(1){ foreach my $num (1..1000){ if($die){return} Glib::Idle->add( sub{ print "\t\t$num\n"; return FALSE; }); select(undef,undef,undef, .1); } } } __END__ #my $timer = Glib::Timeout->add ($interval, $callback, $data=undef, $ +pri- # ority=G_PRIORITY_DEFAULT) # * $interval (integer) number of milliseconds # * $callback (subroutine) # * $data (scalar) # * $priority (integer) # Run $callback every $interval milliseconds until $callback return +s # false. Returns a source id which may be used with # "Glib::Source->remove". Note that a mainloop must be active for # the timeout to execute.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
      Thanks a lot! I'm trying to understand all of that...
        ... the big picture is this..... maintaining smooth event flow in the program's mainloop, and preventing memory usage from climbing ( mem cleanup)

        ... Perl will tend to hang onto memory, figuring if you used it once you will use it again....so tell the system to keep it allocated as yours, even though temporarily it is idle space

        ...and if you try to run a resource heavy video driver from your main program directly, it will bog down and become unresponsive...thus the IPC

        .. the display basics are this....iirc gstreamer will launch in it's own window, but i can't recall how many controls they give you...like stop ,start, rewind, go to frame, etc. .....so you can embed the gstreamer output into one of your own windows, where you can include buttons and enties for control ..... you will have to read the gstreamer docs and examples ( the c library ), and google for gstreamer tutorials

        ... see Perl/Tk front-end to mplayer for a Tk app which would do something similar....however, Wx is a different program...but it should give you some control ideas


        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku
Re: What is the usage of "use Gtk2 -init -threads-init"?
by Anonymous Monk on Nov 02, 2009 at 04:06 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://804402]
Approved by GrandFather
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2017-11-25 01:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (355 votes). Check out past polls.

    Notices?