मैं एक पर्ल स्क्रिप्ट है कि 2 सूत्र, प्रत्येक प्रोसेसर के लिए एक शुरूआत की है। मैं एक धागा समाप्त हो जाती है, तो एक नया एक पैदा की है यह अंत करने के लिए एक धागा के लिए इंतजार करना, की जरूरत है। ऐसा नहीं है कि विधि ब्लॉक कार्यक्रम के बाकी में शामिल होने, इसलिए दूसरा धागा सब कुछ पहले धागा है किया जाता है जो एक तरह से अपने उद्देश्य धरा जब तक समाप्त नहीं हो सकता है।पर्ल में, मैं धागे समानांतर में समाप्त करने के लिए के लिए कैसे इंतजार कर सकते हैं?
मैंने 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
आज आप मेरे भगवान हैं;) अच्छा समाधान .. एक और बात: ताले को सूची में डालने के लिए अब मैं यह करता हूं: अगर (1 == 1) {लॉक (@file_list); $ फ़ाइल = शिफ्ट (@file_list); } मुझे इसकी आवश्यकता है अगर यह कार्य चलाने से पहले अंत में अनलॉक हो जाए। हालांकि यह एक सुंदर नोब-हैक जैसा लगता है :) कोई बेहतर तरीका? – Pmarcoen