2013-04-26 30 views
2

如何在沒有默認中斷點的情況下構建boost.thread。我認爲我的應用程序在預定義的中斷點崩潰。我使用升壓1.53.0與msvc10 構建Boost.Thread不中斷

我有下面的代碼

class IOController { 
public: 
    IOController() { mThread = boost::thread(boost::bind(&IOController::poll, this)); } 
    ~IOController() {mThread.interrupt(); mThread.join() } 

    void doA() { boost::lock_guard<boost::mutex> lock(mMutex); } 

private: 
    void ICanThrow() 
    { 
     try 
     { 
      boost::lock_guard<boost::mutex> lock(mMutex); 
      callFunctionWithSleepFor(); // calling function that can throw and use boost::sleep_for 
     } 
     catch(boost::system_error&) {} 
     catch(std::exception&) {} 
     catch (...) { /* APPLICATION CRASH. */ } 
    } 
    // this is a thread: mThread = boost::thread(&IOController::poll, this) on ctor 
    void poll() 
    { 
     while(true) 
     { 
      callFunctionWithSleepFor();    
      this_thread::sleep_for(some_time); 
     } 
    } 
boost::mutex mMutex; 
boost::thread mThread; 
}; 

現在我正在調用是在一個非常密集的方式調用DOA主線程,並且該類正在另一個線程上進行輪詢。但是有時候我會在ICanThrow中捕捉一個異常(...)。我不知道爲什麼會發生這種情況。但總是從poll()線程中發生。

現在我想嘗試在沒有DONT_PROVIDE_INTERRUPTIONS的情況下構建Boost。有人有一些建議嗎?

+0

也許在IOController的驅動程序中,mThread.interrupt導致ICanThrow()在語句boost :: this_thread :: sleep_for中啓動異常(boost :: thread_interrupted不是從std :: exception派生) ()(具有此陳述的ICanThrow調用函數)。 ICanThrow()在關鍵字(...)中捕獲了這個異常。但是這導致poll()不會中斷。是否有可能 – 2013-04-26 15:20:53

+0

很可能你會遇到與中斷無關的崩潰。 'boost :: thread_interrupted' *是從'std :: exception'派生的。您可以使用'disable_interruption'工具禁用當前線程的中斷:http://www.boost.org/doc/libs/1_53_0/doc/html/thread/thread_management.html#thread.thread_management.this_thread.disable_interruption – 2013-04-26 15:55:52

+0

class BOOST_SYMBOL_VISIBLE thread_interrupted {}; thread_interrupted不會從任何東西(fortunatly)派生。如果thread_interrupted派生frome異常,我們可能會錯誤地捕獲錯誤的異常(就像我所做的那樣) – 2013-04-26 16:19:41

回答

0

問題是,當IOController的析構函數調用mThread.interrupt()時,函數else正在使用sleep_for拋出異常。這個例外是關於caught(...),但只是boost::thread_interrupted一個非派生於任何東西的類。現在 的問題是,thread_interrupted所以阻止應用程序上mThread.join()

相關問題