각 프로세서 당 하나씩 2 개의 스레드를 시작하는 Perl 스크립트가 있습니다. 스레드가 종료 될 때까지 기다려야합니다. 한 스레드가 종료되면 새 스레드가 생성됩니다. join 메소드가 프로그램의 나머지 부분을 차단하는 것처럼 보입니다. 따라서 첫 번째 스레드가 수행하는 모든 작업이 완료 될 때까지 두 번째 스레드가 끝날 수 없습니다.Perl에서 스레드가 병렬로 끝날 때까지 어떻게 기다릴 수 있습니까?
나는 is_joinable
메서드를 시도했지만 그 중 하나도하지 않는 것 같습니다. 여기
use threads;
use threads::shared;
@file_list = @ARGV; #Our file list
$nofiles = $#file_list + 1; #Real number of files
$currfile = 1; #Current number of file to process
my %MSG : shared; #shared hash
$thr0 = threads->new(\&process, shift(@file_list));
$currfile++;
$thr1 = threads->new(\&process, shift(@file_list));
$currfile++;
while(1){
if ($thr0->is_joinable()) {
$thr0->join;
#check if there are files left to process
if($currfile <= $nofiles){
$thr0 = threads->new(\&process, shift(@file_list));
$currfile++;
}
}
if ($thr1->is_joinable()) {
$thr1->join;
#check if there are files left to process
if($currfile <= $nofiles){
$thr1 = threads->new(\&process, shift(@file_list));
$currfile++;
}
}
}
sub process{
print "Opening $currfile of $nofiles\n";
#do some stuff
if(some condition){
lock(%MSG);
#write stuff to hash
}
print "Closing $currfile of $nofiles\n";
}
이의 출력은 다음과 같습니다
Opening 1 of 4
Opening 2 of 4
Closing 1 of 4
Opening 3 of 4
Closing 3 of 4
Opening 4 of 4
Closing 2 of 4
Closing 4 of 4
오늘 너는 내 신이다.) 멋진 해결책 .. 한 가지 더 : 목록에 잠금을 설정하려면 다음을 수행하십시오. if (1 == 1) {lock (@file_list); $ file = shift (@file_list); } 함수를 실행하기 전에 끝에 잠금이 해제되도록하려면이 값이 필요합니다. 이것은 꽤 멍청한 - 해킹처럼 보이지만 :) 더 좋은 방법이 있습니까? – Pmarcoen