viernes, 18 de enero de 2013

Perl Thread and concurrence 1


1) Example of Perl Thread and concurrence:

#!/usr/bin/perl -w
# This is perl 5, version 14

# File name: thread_example_01.pl

use strict;
use Thread qw(:DEFAULT async yield);

my %hash = ( t3 => '', t2 => '', t1 => '', );

for my $key ( keys %hash ) {
        $hash{$key} = Thread->new(\&doSomething, $key)->yield;       
    }

sleep(15);
print "Thread->list = " . Thread->list . "\n";

#Function concurrent
sub doSomething {
    my $thread = shift;
    print "thread $thread - start tid = " . Thread->self->tid . "\n";
    sleep(3);
    print "thread $thread - end tid = " . Thread->self->tid . "\n";
    #yield();
}


Console out:

$ perl thread_example_01.pl
thread t3 - start tid = 1
thread t2 - start tid = 2
thread t1 - start tid = 3
thread t3 - end tid = 1
thread t2 - end tid = 2
thread t1 - end tid = 3
Thread->list = 3
Perl exited with active threads:
    0 running and unjoined
    3 finished and unjoined
    0 running and detached



References:

No hay comentarios:

Publicar un comentario