#!/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 returns # 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.