#!/usr/bin/perl -w
use strict;
use warnings;
use threads;
use threads::shared;
my $var: shared;
$var = 0;
my @threads;
for(1..10) {
new threads(\&myThread, $_);
}
$_->join for threads->list;
sub myThread
{
my $thrn = shift;
printf "thread %d started at %s\n", $thrn, scalar localtime time;
while($var < 100) {
#printf "thread #%d: var=%d\n", $thrn, $var;
$var++;
sleep 1;
}
printf "thread %d stopped at %s\n", $thrn, scalar localtime time;
}
leon@leon-desktop:/opt/lampp/htdocs/allmuz/application/scripts$ ./autocomments.pl
thread 1 started at Wed Aug 11 11:52:20 2010
thread 2 started at Wed Aug 11 11:52:20 2010
thread 3 started at Wed Aug 11 11:52:20 2010
thread 4 started at Wed Aug 11 11:52:20 2010
thread 5 started at Wed Aug 11 11:52:20 2010
thread 6 started at Wed Aug 11 11:52:20 2010
thread 7 started at Wed Aug 11 11:52:20 2010
thread 8 started at Wed Aug 11 11:52:20 2010
thread 9 started at Wed Aug 11 11:52:20 2010
thread 10 started at Wed Aug 11 11:52:20 2010
thread 1 stopped at Wed Aug 11 11:52:30 2010
thread 2 stopped at Wed Aug 11 11:52:30 2010
thread 3 stopped at Wed Aug 11 11:52:30 2010
thread 4 stopped at Wed Aug 11 11:52:30 2010
thread 5 stopped at Wed Aug 11 11:52:30 2010
thread 6 stopped at Wed Aug 11 11:52:30 2010
thread 7 stopped at Wed Aug 11 11:52:30 2010
thread 8 stopped at Wed Aug 11 11:52:30 2010
thread 9 stopped at Wed Aug 11 11:52:30 2010
thread 10 stopped at Wed Aug 11 11:52:30 2010
Можно видеть, что все выполнилось за 10 секунд.
Комментариев нет:
Отправить комментарий