Hi monks!

I'm pretty new to Perl and am having problems with threads. I'm writing a benchmarking script that needs to create many different threads where each thread will run an external program (a script interpreter (not Perl) running different scripts) multiple times.

The script ends prematurely with the following errors:

Thread already joined at pm_spawn.pl line 72.
A thread exited while x threads were running.
and sometimes it seems to just hang randomly (and in different places). Any ideas what I'm doing wrong? I don't think the problem is from the external program, as the scripts that they're running simply print a character to the command line (for the moment anyway). I should probably add that I'm running Windows 2000.

Here's the script in it's entirety:

----------------BOF----------------
use Acme::Comment type => 'C++', own_line => 1, one_line => 1; use Cwd; use Run; use Config; use Data::Dumper; use threads; //use Async; use Benchmark qw(:all); use strict; use warnings; $Config{useithreads} or die("Recompile Perl with threads to run this p +rogram."); # Change this structure accordingly... my %allScripts = ( submitters => { numRuns => 4, steps => ["tssubmitter_1.script", "tssubmitter_2.script"] }, reviewers => { numRuns => 1, steps => ["tsreviewer_1.script", "tsreviewer_2.script"] } ); my $numCompleteTestRuns = 100; my $scriptInterpreter = "ScriptRunner.exe"; my $currentDir = getcwd(); my @threadList = (); timethis($numCompleteTestRuns, 'runTests'); sub runTests { print "Waiting to spawn processes.\n"; foreach my $userType (sort {$allScripts{$a} <=> $allScripts{$b}} k +eys %allScripts) { // print Dumper($allScripts{$userType}{steps}); print "\nThe $userType will run $allScripts{$userType}{numRuns +} times. The steps are:\n"; foreach my $i (0 .. $#{ $allScripts{$userType}{steps}}) { print "$i = $allScripts{$userType}{steps}[$i]\n"; } } print("\n\n"); # key-value pairs in a Perl hash are not always stored in the orde +r in which they were defined! // foreach my $userType (keys %allScripts) foreach my $userType (sort {$allScripts{$a} <=> $allScripts{$b}} k +eys %allScripts) { for my $i (0 .. $allScripts{$userType}{numRuns}) { print "\nAbout to create $userType thread $i:\n"; my $thread = threads->new(\&stepThread, \$allScripts{$user +Type}); push (@threadList, $thread); } } my $numThreads = $#threadList + 1; print("There are $numThreads threads\n\n"); # wait for each running thread to end foreach my $thread (@threadList) { $thread->join; print("A thread joined\n\n"); } print "Processes ended - Goodbye.\n"; } # thread runs a type of users steps once only sub stepThread { print("starting thread\n\n"); my $currentUsertype = shift; // print Dumper(${$currentUsertype}->{steps}); foreach my $i (0 .. $#{${$currentUsertype}->{steps}}) { my $fileName = ${$currentUsertype}->{steps}[$i]; my $command = $scriptInterpreter . " \"" . $currentDir . "/" . + $fileName . "\"\n"; print("About to run: $fileName\n"); my $time0 = new Benchmark; my $output = `$command`; my $time1 = new Benchmark; my $timeTaken = timestr(timediff($time1, $time0)); my $printOutput = "******** BEGIN $fileName output ********\n$ +output\n******** END $fileName output (time taken: $timeTaken) ****** +**\n"; print($printOutput); } print("Ending thread\n\n"); }
----------------EOF----------------

In reply to "Thread already joined at..." and "A thread exited while x threads were running" errors by nickos

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.