如何确定完成哪个线程

时间:2017-04-16 19:14:19

标签: c++ multithreading pthreads

我有一个调用pthread_join的循环,但循环的顺序与线程终止的顺序不匹配。 如何监控线程完成然后调用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;
        }

1 个答案:

答案 0 :(得分:0)

  

如何监控线程完成然后调用join?

让联接检测完成。 (即什么都不做特别)

  

我有一个调用pthread_join的循环,但是循环的顺序与线程终止的顺序不匹配。

循环的顺序无关紧要。

a)thread[main]调用thread[1].'join'只会暂停,直到thread[1]退出。之后,将允许线程[main]继续循环的其余部分。

b)当thread[2]thread[1]之前终止时,调用thread[main]的{​​{1}}只会立即返回。再次,thread[2].join继续。

c)确保thread[main]thread[1]之前终止(以匹配循环序列)的努力是一项令人惊讶的耗时工作,没有任何好处。

正在更新...正在寻找我认为已提交的代码。