I'm wanting to run 10 threads, doing their thing, and when one finishes, start up another one. Thus keeping 10 going at all times until the end of looping through a list.
For example code below:
#!/usr/bin/perl
use threads;
sub mythread
{
my $arg1 = shift;
my $arg2 = shift;
my $myself = threads->self;
my $threadid = $myself->tid;
print "Thread $threadid with $arg1 and $arg2 to do something...\n";
sleep 30;
}
MYLOOP: foreach (0 ... 100)
{
print "checking thread count...";
if (threads->list == 10) {sleep 30; redo MYLOOP;}
$a=$_;
$b=$_ + 10;
push @threads, threads->create("mythread",($a,$b));
}
When I run this, however, it blows chunks:
# ./threadtest.pl
checking thread count...Thread 1 with 0 and 10 to do something...
Can't call method "tid" without a package or object reference at /usr/opt/perl5/lib/5.8.2/aix-thread-multi/threads.pm line 57.
checking thread count...A thread exited while 2 threads were running.
What am I doing wrong with this approach?
Thanks,
---
Batavus
For example code below:
#!/usr/bin/perl
use threads;
sub mythread
{
my $arg1 = shift;
my $arg2 = shift;
my $myself = threads->self;
my $threadid = $myself->tid;
print "Thread $threadid with $arg1 and $arg2 to do something...\n";
sleep 30;
}
MYLOOP: foreach (0 ... 100)
{
print "checking thread count...";
if (threads->list == 10) {sleep 30; redo MYLOOP;}
$a=$_;
$b=$_ + 10;
push @threads, threads->create("mythread",($a,$b));
}
When I run this, however, it blows chunks:
# ./threadtest.pl
checking thread count...Thread 1 with 0 and 10 to do something...
Can't call method "tid" without a package or object reference at /usr/opt/perl5/lib/5.8.2/aix-thread-multi/threads.pm line 57.
checking thread count...A thread exited while 2 threads were running.
What am I doing wrong with this approach?
Thanks,
---
Batavus