Ярлыки

.htaccess (4) тестирование (8) шаблоны проектирования (3) css (5) Debian (6) docker (2) Doctrine2 (6) Git (6) html (4) java (6) javascript (13) jquery (11) LFS (3) linux (23) mac os (4) mod_rewrite (2) MSSQL (4) MySQL (18) ORM Doctrine (17) patterns (3) PDO (3) perl (7) PHP (64) PHPUnit (8) Python (15) SEO (2) Silex (1) SimpleXML (1) SQL (14) ssh (4) Ubuntu (24) Yii1 (1) Zend Framework (19) ZendFramework2 (8)

среда, 11 августа 2010 г.

Perl. Потоки.



#!/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 секунд.

Комментариев нет:

Отправить комментарий