2009-09-03 22 views
5

Birisi, aşağıdaki kodu değiştirerek iş parçacığı arasındaki özel durumları aktarmak için Boost istisna kitaplığını nasıl kullanabileceğine dair basit ancak tam bir örnek gösterebilir mi?C++ Yükseltme Örneği Konu arasında bir istisna atma örneği Örnek

Uyguladığım şey basit bir çok iş parçacıklı Delegate modelidir.

class DelegeeThread 
{ 
public: 
    void operator()() 
    { 
    while(true) 
    { 
     // Do some work 

     if(error) 
     { 
     // This exception must be caught by DelegatorThread 
     throw std::exception("An error happened!"); 
     } 
    } 
    } 
}; 

class DelegatorThread 
{ 
public: 
    DelegatorThread() : delegeeThread(DelegeeThread()){} // launches DelegeeThread 
    void operator()() 
    { 
    while(true) 
    { 
     // Do some work and wait 

     // ? What do I put in here to catch the exception thrown by DelegeeThread ? 
    } 
    } 
private: 
    tbb::tbb_thread delegeeThread; 
}; 
+0

Vay canına ... posttan 10 saat sonra ve kimse cevap vermedi mi? sorumu kötü bir şekilde mi söyledim, yoksa bu sorun mu zor? – sivabudh

+1

Uygulamanıza ne derseniz, beklediğiniz şey olmayabilir. DelegeeThread, diğer iş parçacığında bir istisna tetiklemek istediğinde, Yetkilendiren ilgisiz bir çalışma yürütecektir veya zaten sona erdirilmiş olabilir, bu nedenle yakalama gecikebilir veya hiç gerçekleşmeyebilir. – asveikau

+0

emin, bahsettiğiniz noktaya katılıyorum. – sivabudh

cevap

4

Temsilcinin eşzamansız olarak ayrı bir iş parçacığı üzerinde yürütülmesini istediğinizi kabul ettim. İşte boost ipliklerini ve istisnaları kullanarak örnek:

#include <boost/exception/all.hpp> 
#include <boost/thread.hpp> 
#include <boost/bind.hpp> 
#include <iostream> 

class DelegeeThread 
{ 
public: 
    void operator()(boost::exception_ptr& excPtr) 
    { 
     try 
     { 
      int counter = 0; 
      while(true) 
      { 
       // Do some work 

       if(++counter == 1000000000) 
       { 
        throw boost::enable_current_exception(std::exception("An error happened!")); 
       } 

      } 
     } 
     catch(...) 
     { 
      // type of exception is preserved 
      excPtr = boost::current_exception(); 
     } 
    } 
}; 

class DelegatorThread 
{ 
public: 
    DelegatorThread() : 
     delegeeThread(boost::bind(&DelegeeThread::operator(), boost::ref(delegee), boost::ref(exceptionPtr))) 
     { 
      // launches DelegeeThread 
     } 

    void wait() 
    { 
     // wait for a worker thread to finish 
     delegeeThread.join(); 

     // Check if a worker threw 
     if(exceptionPtr) 
     { 
      // if so, rethrow on the wait() caller thread 
      boost::rethrow_exception(exceptionPtr); 
     } 
    } 

private: 
    DelegeeThread   delegee; 
    boost::thread   delegeeThread; 
    boost::exception_ptr exceptionPtr; 
}; 


int main() 
{ 
    try 
    { 
     // asynchronous work starts here 
     DelegatorThread dt; 

     // do some other work on a main thread... 

     dt.wait(); 

    } 
    catch(std::exception& e) 
    { 
     std::cout << e.what(); 
    } 

    system("pause"); 
    return 0; 
} 
+0

Teşekkürler. Mükemmel cevap. – sivabudh

2

Bu sorunu çözmek için Boost :: İstisna kullanmak isteyebilirsiniz. Burada çağıran iş parçacığı istisnası almak için onların istisna lib kullanmak için bir örnek: http://www.boost.org/doc/libs/1_40_0/libs/exception/doc/tutorial_exception_ptr.html

İyi hatırlarsam, C++ 0x bu sorunu çözmek için benzer bir şeye izin verecek bir mekanizma sağlayacaktır.

+0

evet, Boost :: Exception uygulamasını kullanmamız gerektiğine katılıyorum. Zaten bir örneğe baktım, ama nasıl kullanacağımı gerçekten anlamadım. – sivabudh