私はループを持っていますが、pthread_join
を呼び出しますが、ループの順序はスレッドの終了の順序と一致しません。 どうすればスレッドの完了を監視してから、参加を呼び出すことができますか?どのスレッドが完了したかを調べる方法
for (int th=0; th<sections; th++)
{
cout<<"start joining "<<th<<endl<<flush;
result_code = pthread_join(threads[th] , (void**)&status);
cout<<th<<" join error "<<strerror(result_code)<<endl<<flush;
cout<<"Join status is "<<status<<endl<<flush;
}
これは最初の 完了のスレッドにサービスを提供することにより、マルチスレッドのスループットを最大化するようで、私のソリューションです。この解決策は、pthread_joinループの順序に依存しません。私が参加を呼び出し、スレッドの完了を監視することができますどのように
// loop & wait for the first done thread
std::bitset<Nsections> ready;
std::bitset<Nsections> done;
ready.reset();
for (unsigned b=0; b<sections; b++) ready.flip(b);
done = ready;
unsigned currIdx = 1;
int th = 0;
int th_= 0;
int stat;
while (done.any())
{
// main loops waiting for 1st thread to complete.
// completion is checked by global vector
// vStatus (singlton write protected)
// and not by pthread_exit returned value,
// in ordder to maximize throughput by
// post processig the first
// finished thread.
if ((obj.vStatus).empty()) { Sleep (5); continue; }
while (ready.any())
{
if (sections == 1) break;
if (!(obj.vStatus).empty())
{
if (currIdx <= (obj.vStatus).size())
{
th_ = currIdx-1;
std::string s =
ready.to_string<char,std::string::traits_type,std::string::allocator_type>();
cout<<"checking "<<th_<<"\t"<<s<<"\t"
<<(ready.test(th_)?"T":"F")<<"\t"<<(obj.vStatus)[th_].retVal <<endl;
if ((obj.vStatus)[th_].retVal < 1)
{
if (ready.test(th_))
{
th=th_;
ready.reset(th);
goto retry;
}
}
}
}
Sleep (2);
} // while ready
retry:
cout<<"start joining "<<th<<endl<<flush;
result_code = pthread_join(threads[th] , (void**)&status);
switch (result_code)
{
case EDEADLK: goto retry; break;
case EINVAL:
case ESRCH:
case 0:
currIdx++;
stat = status->retVal;
free (status);
done.reset(th);
std::string s =
done.to_string<char,std::string::traits_type,std::string::allocator_type>();
cout<<"joined thread "<<th<<"\t"<<s<<"\t"
<<(done.test(th)?"T":"F")<<"\t"<<stat <<endl;
while (true)
{
auto ret=pthread_cancel (threads[th]) ;
if (ret == ESRCH) { netTH--; break; }
Sleep (20);
}
break;
}
これにより、いくつかのフラグを確認する必要が生じる場合があります。 'std :: async'や' std :: future'を生成する他のメカニズムなど、抽象度の高いレベルを使用してみませんか?スレッドが完了したらboolを設定すると、http://stackoverflow.com/questions/10890242/get-the-status-of-a-stdfuture – WhiZTiM
を参照してください。メインスレッドのループでboolをチェックしてください – DanielCollier
ダニエルありがとうございますが、pthread_createのargであるbool varは終了前に更新されていないと思います – BNR