R
robert.waters
Hi,
I have a function that does a repetitive task, each task identified by
a unique index; I would like this task to be shared amongst a number of
threads. My question is, in what manner should I call 'join' in order
to be sure that each thread has completed it's task before the end of
the program?
My current logic is this:
--------------------------------------------------------------------
use LWP::UserAgent;
use HTTP::Request;
use threads;
use threads::shared;
my @threads;
my $threadcnt = 20;
my $unique_index : shared = 1;
$unique_index = 1;
my $top_index = 1000;
# CREATE ALL THE THREADS
for (my $i=0;$i<$threadcnt;$i++) {
$threads[$i] = threads->new('workerFunction', $i);
}
# JOIN ALL THREADS
foreach $thread (threads->list) {
$thread->join;
}
print "done.\n";
sub workerFunction {
my $threadindex = shift;
print STDERR "thread $threadindex starting...";
my $ua = LWP::UserAgent->new;
while ($unique_index < $top_index) {
my $url = "http://something.com/index.php?tid=".$unique_index++;
my $req = HTTP::Request->new(GET => $url);
my $resp = $ua->request($req);
print $resp->content;
}
}
-----------------------------------------
Basically, each thread shares a variable representing the index (and
increments it each time it is used).
After all the threads have been created, I iterate through the thread
list, calling join on each one in order. This iteration seems hackish
to me though; can this really be the right way to go about waiting for
the threads to finish? If thread 6 finishes first (which is definitely
possible), I am still waiting for threads 1 through 5 to finish before
I ever even check to see if thread 6 completes.
This technique, while working fine enough, just does not seem to be
"The Right Way" to do it; I would assume that the process of waiting
for threads to finish should be asynchronous.
Any suggestions?
Thank you-
Robert
I have a function that does a repetitive task, each task identified by
a unique index; I would like this task to be shared amongst a number of
threads. My question is, in what manner should I call 'join' in order
to be sure that each thread has completed it's task before the end of
the program?
My current logic is this:
--------------------------------------------------------------------
use LWP::UserAgent;
use HTTP::Request;
use threads;
use threads::shared;
my @threads;
my $threadcnt = 20;
my $unique_index : shared = 1;
$unique_index = 1;
my $top_index = 1000;
# CREATE ALL THE THREADS
for (my $i=0;$i<$threadcnt;$i++) {
$threads[$i] = threads->new('workerFunction', $i);
}
# JOIN ALL THREADS
foreach $thread (threads->list) {
$thread->join;
}
print "done.\n";
sub workerFunction {
my $threadindex = shift;
print STDERR "thread $threadindex starting...";
my $ua = LWP::UserAgent->new;
while ($unique_index < $top_index) {
my $url = "http://something.com/index.php?tid=".$unique_index++;
my $req = HTTP::Request->new(GET => $url);
my $resp = $ua->request($req);
print $resp->content;
}
}
-----------------------------------------
Basically, each thread shares a variable representing the index (and
increments it each time it is used).
After all the threads have been created, I iterate through the thread
list, calling join on each one in order. This iteration seems hackish
to me though; can this really be the right way to go about waiting for
the threads to finish? If thread 6 finishes first (which is definitely
possible), I am still waiting for threads 1 through 5 to finish before
I ever even check to see if thread 6 completes.
This technique, while working fine enough, just does not seem to be
"The Right Way" to do it; I would assume that the process of waiting
for threads to finish should be asynchronous.
Any suggestions?
Thank you-
Robert