Thread parallele filehandles (24 answers)
Opened by Gast at 2006-08-28 12:57

Gast Gast
 2006-08-28 12:57
#69275 #69275
problem abstract: filehandles in threads
so im prinzip funktioniert der code - solange es nur einen thread gibt. Bei mehreren Threads hängt sich das programm auf. Wenn ich statt open() system() verwende funktionieren auch mehrere threads wunderbar - leider will ich aber den output des programms pipen und weiterverarbeiten. Da ich in jedem thread "my $fh" verwende, also ein scalar, sollte der auch für jeden thread unique sein.. irgendwie klappts aber halt doch nicht - lock problem? lösungsvorschläge? :-)

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/perl -w

use strict;
use threads;

sub SubFunction;

print "main: ".threads->tid."\n";
my $thread;
$thread = threads->new(\&SubFunction, "localhost");
$thread = threads->new(\&SubFunction, "localhost");
$thread = threads->new(\&SubFunction, "localhost");
$thread = threads->new(\&SubFunction, "localhost");
$thread = threads->new(\&SubFunction, "localhost");

# loop through threads and wait for remaining
foreach $thread (threads->list) {
#print "remaining threads: ".threads->list."\n";
if ($thread->tid && !threads::equal($thread, threads->self)) {
$thread->join; # join childs only
}
}


sub SubFunction {
print "- thread: ".threads->tid."\n";
#system("ping $_[0]");

my $fh;
open($fh, '-|', "ping.exe $_[0]") or die "couldn't open pipe!";
print while (<$fh>);
close($fh);
}

View full thread parallele filehandles