簡體   English   中英

如何確定哪個線程完成

[英]How to determine which thread is done

我有一個調用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;
        }

如何監視線程​​完成情況然后調用join?

通過讓加入檢測完成。 (即沒什么特別的)

我有一個調用pthread_join的循環,但是循環的順序與線程終止的順序不匹配。

循環的順序無關緊要。

a) thread[main]調用thread[1].'join'將被暫停直到thread[1]退出。 之后,將允許thread [main]繼續循環的其余部分。

b)當thread[2]thread[1]之前終止時,調用thread[2].join thread[main]立即返回。 同樣, thread[main]繼續。

c)確保thread[1]thread[2]之前終止(以匹配循環序列)的工作是令人驚訝的耗時工作,沒有任何好處。

更新進行中...正在尋找我認為已經提交的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM