I appreciate that BrowserUK is attempting to help, I'm not knocking that at all (yes-- thank you VERY MUCH BrowserUK). As I just posted in my reply to him-- the Re^2 code is exactly what I'm using to test with:
#! perl -slw
use strict;
use threads;
sub test {
my $t = shift;
my $cmd = "c:/windows/system32/sort.exe test$t.dat";
print "Thread $t\n"; # show things are in motion...
sleep 1; # kludge to insure all threads are up...
system "$cmd >sorted$t.dat";
open F, "<sorted$t.dat" or die $!;
print "Via system:\n", do{ local $/; <F> };
close F;
print "Via backticks\n", `$cmd`;
open F, "$cmd|" or die $!;
print "Via pipe\n", <F>;
close F;
}
sub spawnem
{
my $t1 = async{ \&test(1); };
my $t2 = async{ \&test(2); };
my $t3 = async{ \&test(3); };
my $t4 = async{ \&test(4); };
$t1->join;
$t2->join;
$t3->join;
$t4->join;
}
&spawnem;
The test1-4.dat files all contain the same two lines
world
hello
And I loop the test from the DOS shell with the following .bat script:
LOOP:
perl thread2.pl
goto LOOP
A typical output I get when it hangs immediately looks like this:
Thread 1
Thread 2
Thread 3
Thread 4
Via system:
hello
world
where it just sits there and hangs until I ctl-C.
It'd be nice if the problem is a bug in my code, but I had started originally with a larger example and BrowserUK neatly reduced it to a short little thing that worked for him but didn't run multiple threads in parallel. So I modified it so that it would and that's where the code came from. Again, I may have introduced a bug and if so I'd sure like to know where my conception of how threads works is faulty...
-- Sync
|