Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
991 views
in Technique[技术] by (71.8m points)

multithreading - How can I use Perl's system call to spawn independent threads?

I would like to call other Perl scripts in order to perform a contention test from with a main Perl script.

Something like this currently works:

system("perl 1.pl");
system("perl 2.pl");
exit;

However, I would like to kick these off as independent threads running at the same time.

I tried, based on my Google searches, doing something like this:

system(1, "perl 1.pl");
system(1, "perl 2.pl");
exit;

That doesn't work. The main script exists immediately, which is fine, but the underlying threads I want to spawn don't get kicked off. I was wondering if there was something else I have to do or if anyone else has done something like this.

Thanks for any help in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
use threads;
$thr1 = threads->create('msc', 'perl 1.pl');
$thr2 = threads->create('msc', 'perl 2.pl');

$thr1->join();
$thr2->join();

sub msc{ ## make system call
  system( @_ );
}

This will wait for both to finish executing before the it will exit. I'm guessing this is what you want from your original question, right? If not feel free to drop a comment and edit your post to explain it better, and I'll try again.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...