my @data = qw( 1 2 3 4 5); modo(); sub modo { # Do stuff, then reschedule myself. my $newdata = shift @data; if ( defined $newdata){ $MW->after($MILLISECOND_DELAY, [ \&modo, $newdata ] ); } } #### #!/usr/bin/perl use warnings; use strict; use threads; use threads::shared; use Glib qw/TRUE FALSE/; #setup shared hash my %shash; #share(%shash); #will work only for first level keys my $numworkers = 3; foreach my $dthread(1..$numworkers){ share ($shash{$dthread}{'go'}); share ($shash{$dthread}{'work'}); share ($shash{$dthread}{'die'}); share ($shash{$dthread}{'work'}); $shash{$dthread}{'go'} = 0; $shash{$dthread}{'work'} = ''; $shash{$dthread}{'die'} = 0; $shash{$dthread}{'work'} = 0; } my $finished = 0; my $main_loop = Glib::MainLoop->new; my $x = 0; my $y = 0; #create 3 sleeping thread passing it info for my $num(1..3){ my $thread = threads->new(\&work,$num, $x, $y); } #start threads $shash{1}{'go'} = 1; $shash{2}{'go'} = 1; $shash{3}{'go'} = 1; #timer to watch for threads finishing my $timer1 = Glib::Timeout->add (100, \&timer1_callback, undef, 1 ); sub timer1_callback{ if( $shash{1}{'work'} == 10 and $shash{2}{'work'} == 10 and $shash{3}{'work'} == 10 ){ print "\t\t\t\t\tFINISHED!!\n"; clean_exit(); # return 0; #stop timer }else{ return 1;} #keep timer going } # do something in parent thread my $count = 1; my $timer = Glib::Timeout->add (10, \&timer_callback, undef, 1 ); sub timer_callback{ $count++; print "main $count\n"; if($finished){return 0}else{return 1} } $main_loop->run; sub clean_exit{ $shash{1}{'die'} = 1; $shash{2}{'die'} = 1; $shash{3}{'die'} = 1; exit; } sub work{ my ($num,$x,$y) = @_; $|++; my $pstring; for(1..$num){$pstring .= "\t"} foreach my $z (1..10){ Glib::Idle->add( sub{ if($shash{$num}{'die'} == 1){ return }; print "$pstring thread.$num->$z\n"; $shash{$num}{'work'} = $z; return FALSE; }); select(undef,undef,undef, .1); } }