hi guys..
im totally new with perl and was given an urgent task to make a multi-threading perl script...
problem:
i need to create multiple number of threads out of one... and fire them all up at once. i need to get the timing to make sure if all the process started simultaneously.
i came up with a module "Thread::Barrier" but, i don't have enough resources on how to use it.... been googling this module lately but i haven't seen anyone using it... i couldn't clearly understand the examples shown in this module...
for sure i used it in a wrong way...
objectives:
1. create multiple number of threads (user input) out of 1.
2. fire them all up at once..
3. get the timing to prove that these threads are all
executed at the same time.
i tried making a script.... with just simple print as a process.... and i also used system "date" to check if the processes were executed at the same time....
can lead me to the right path guys... thanks
#!/usr/bin/perl
use warnings;
use threads;
use threads::shared;
use Thread::Barrier;
my $br = Thread::Barrier->new;
my $thr1 = threads->new(\&req);
$thr1->join;
sub req
{
$count=0;
print "Enter number: ";
$num = <STDIN>;
my @thrlist;
for (1..$num) #create number of threads
{
$thrlist[$_] = threads->new(sub { print"test\n"; syste
+m("date"); });
$thrlist[$_]->join;
$count++;
}
foreach (threads->list){
$br->wait; }
print $br->wait;
print "\n\ntotal number of threads $count\n\n";
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.