この関数は、ofstream を参照として受け取り、構造体パケットを構築し、ofstream を使用して構造体からトライ一致クラスへのスレッドを作成します。スタックは、マッチ距離の順に n 個のマッチで返されます。次に、ofstream、パケット、およびスタックは、一致を同じ出力ファイルに書き込む print 関数への参照によって渡されます - ofstream が使用されていないときを待ちます。問題は、マッチの半分が書き込まれた後に ofstream がクラッシュすることです。
ofstream、パケット、および outfile ヘッダー
void Match_Import_Start(Trie & tri)
{
stack<Trie::MatchesT> out;
ofstream myfile;
Trie::MatchesT matchSet;
myfile.open(outFile.c_str() );
myfile << "DESCRIPTION,SUGGESTED.DESCRIPTION,EDIT" << "\n"; //write header
myfile.close();
Match_Import (tri, myfile, out, matchSet);
return;
}
レコード リストからスレッドを生成する
void Match_Import(Trie &tri, ofstream &myfile, stack<Trie::MatchesT> out, Trie::MatchesT matchSet)
{
out=parse_CSV_file(timeFile); //read in records
settingT x;
x.score=0;
boost::thread_group tgroup; //http://stackoverflow.com/questions/8744279/create-threads-in-a-loop
while (!out.empty() ) {
matchSet=out.top();
out.pop();
tgroup.create_thread(boost::bind( MaxDistanceCorrections, boost::ref(tri), matchSet, boost::ref(myfile), boost::ref(x) ) );
}
tgroup.join_all();
return;
}
マッチからの有効な復帰を確認する
void MaxDistanceCorrections(Trie & tri, Trie::MatchesT matchSet, ofstream &myfile, settingT &x)
{
if (!matchSet.candidateStack.empty() ) ) {
matchSet.candidateStack.sort(compareCorrMain);
PrintCorrections(tri, matchSet, myfile, x);
return;
} else {
tri.suggest(matchSet); //send out to trie match
if (matchSet.candidateStack.empty() ) { }// modify match parameters
MaxDistanceCorrections(tri, matchSet, myfile, x);
}
}
ofstream が利用可能になったときに出力します
void PrintCorrections(Trie &tri, Trie::MatchesT &matchSet, ofstream &myfile, settingT &x)
{
while (true) {
if (!myfile.is_open() ) {
myfile.open(outFile.c_str(), ios::out | ios::app);
break;
}
}
while (!matchSet.candidateStack.empty() ) {
Trie::CorrectionT corr=matchSet.candidateStack.back();
matchSet.candidateStack.pop_back();
const bool flagGood=scoreSuggest (corr); //score
if (flagGood ) x.score++;
myfile << matchSet.testCase << "," << corr.match << "," << corr.editDistance << "\n";
}
myfile.close();
return;
}
マルチスレッドではかなり新しい機能ですが、これらの関数はシングル スレッドとして問題なく動作しました。
ofstream available のチェックは、候補の一致をスピンオフする while ループ内に配置する必要がありますか? ofstream が利用可能になると、印刷シーケンスを開始すると、他のスレッドから ofstream を結び付ける必要があります。
複数のスレッドで共有される ofstream の使用を予約するより良い方法はありますか?