Thread
From Yefu's notes
一比较全面的介绍见这里
#!/usr/bin/perl use threads; use threads::shared; use Time::HiRes qw(usleep nanosleep); my $uprate:shared = 1; my $uptime:shared=1; my $downtime:shared=1; my $period=20; threads->new(\&get_uprate); while(1) { if( $uptime/($uptime + $downtime) > $uprate) { system 'killall httpd -s SIGSTOP'; $downtime++; } else { system 'killall httpd -s SIGCONT'; $uptime++; } usleep $period; } sub get_uprate { while(1) { $uprate=`cat /tmp/rate`; chomp $uprate; print "$uprate $uptime $downtime\n"; $uptime=1; $downtime=1; } }
我觉得最好的例子是这个:
#!/usr/bin/perl use threads; use threads::shared; $|=1; my ($global):shared; my (@threads); push(@threads, threads->new(\&mySub,1)); push(@threads, threads->new(\&mySub,2)); push(@threads, threads->new(\&mySub,3)); $i = 0; foreach my $myThread(@threads) { my @ReturnData = $myTread->join ; print "Thread $i returned: @ReturnData\n"; $i++; } sub mySub { my ($threadID) = @_; for(0..10) { $global++; print "Thread ID: $threadID >> $_ >> GLB: $global\n"; sleep(1); } return( $id ); }

