Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need threads help

Status
Not open for further replies.

batavus

Programmer
Jun 13, 2003
21
US
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
 
Point 1: Always "use strict;".
Point 2: How can you expect to call a method "tid" on "$myself" when you haven't defined "$myself"?
Point 3: I think you'll find the "tid" is a thread object method and therefore you can only call it from the parent process using the thread object returned to that parent process.


Trojan.
 
Thanks for the response TrojanWarBlade. But, if I take tid call out of my code, it still blows chunks:

#!/usr/bin/perl

use threads;

sub mythread
{
my $arg1 = shift;
my $arg2 = shift;

print "Thread with $arg1 and $arg2 to do something...\n";
system(sleep 30);
}

MYLOOP: foreach (0 ... 100)
{
if (threads->list == 10) {sleep 30; redo MYLOOP;}
$a=$_;
$b=$_ + 10;
$myt = threads->create("mythread",($a,$b));
}


# ./threadtest.pl
Thread 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.
A thread exited while 2 threads were running.


Notice where it is failing the method call to "tid", not in my code but un the threads.pm package/module.

What I'm thinking at the moment is to just detach every thread, then somehow keep track of them outside of the threads module. Maybe by creating /tmp empty file entries while the thread is running, then the thread would remove the empty file in /tmp before it returns. Thus the 'parent' program can keep track of threads by checking /tmp for those file entries. Not elaborite, but should work for the single use functionality I'm looking for in this script.

Anyone else have some suggestions?

Thanks,

---
Batavus
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top