On Mon, 24 Feb 2003 10:45:44 -0800 (PST), [EMAIL PROTECTED] (Madhu
Reddy) wrote:

>Hi,
>   does anybody have sample perl script  multi
>threaded program ....

Here are a couple of simple examples.
When you run them, open an xterm an watch "top"
while they run.
###################################################
#!/usr/bin/perl
use threads;
my @kiddies;

foreach(1..10)
{
print "$$ starting loop $_";

push @kiddies, threads->new(\&sub1);

print "$$ exiting loop $_\n";
}

sub sub1
{
print "\tchild ", threads->tid(), " created ok\n";
sleep(int(rand(10)));
print "\tchild", threads->tid() , " done, outta here\n";
}

foreach (@kiddies){ $_->join(); }
####################################################


####################################################
#!/usr/bin/perl
#threads share same memory stack
#each thread has it's own pid and stack pointer
#threads can access other threads variables
use threads;
use threads::shared;
use strict;
my $wait = 500;
my @up: shared;
my $th;
my @childs;
my $child;
my @down: shared;

my @list = qw (5 10 15 20 25 30 35 40 45 50 55 60);

foreach (@list) {
        push @childs, threads->create( "do_my_thing", "$_" );
        }

foreach $child (@childs) {
           $child->join();
         }

print @up,"\n";
print @down,"\n";

sub do_my_thing {
    my $val = shift;
    push @up,"$val started";
    sleep $val; #sleep various lengths in thread
    push @down, "$val down\n";
}

###############################################################





-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to