Re: creating simaltaneous threads

[email protected] (Rob Dixon)
Newsgroups perl.beginners
Message-ID <[email protected]>
perl pra wrote:
> 
> I want to create 100 threads/processes  simaltaneoulsy (at the same time),
> and those 100 threads/processes  should  execute a subrountine at the same
> time.
> 
> can anybody help me giving some ideas reagarding this, I tried creating this
> 100  process using fork in a for loop but that does not server the purpose.
> 
> I even tried with Parallel::ForkManager; but it did not work for me.
> 
> Please help me in this.

Hi Siva

Take a look at the 'threads' pragma. The sample program below may help you.

Rob


use strict;
use warnings;

use threads;

sub threadsub {
  my $val = shift;
  print "Thread $val\n";
}

my @threads;

for (1 .. 100) {
  my $thr = threads->create(\&threadsub, $_);
  push @threads, $thr;
}

$_->join foreach @threads;
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.