2011-08-07 9 views
2

スレッドが特定のスコープ内にあるときにスレッドの中断を防止しようとしています。ただし、boost::this_thread::disable_interruption di()を使用しても影響はありません。boost disable_interruption thread_interruptedを防止しない

#include <boost/thread.hpp> 
#include <iostream> 

void worker() { 
    std::cout << "START" << std::endl; 

    for(;;) { 
     { 
      boost::this_thread::disable_interruption di(); 
      try { 
       boost::this_thread::sleep(boost::posix_time::seconds(1)); 
      } 
      catch(boost::thread_interrupted & e) { 
       assert(false); 
      } 
     } 

     try { 
      boost::this_thread::interruption_point(); 
     } 
     catch(boost::thread_interrupted & e) { 
      break; 
     } 

    } 

    std::cout << "END" << std::endl; 
} 


int main() { 
    boost::thread thread(&worker); 
    thread.interrupt(); 
    thread.join(); 
} 

ドキュメントはdiがスコープ内にある間、boost::this_thread::sleep()boost::thread_interruptedを投げないであろうことを意味すると思われます。

私は間違っていますか?

答えて

5

あなたが次の行に括弧を削除する必要があります。その代わりdisable_interruptionオブジェクトを作成する

//boost::this_thread::disable_interruption di(); 
boost::this_thread::disable_interruption di; 

、関数ジを宣言しました。

+0

ありがとう、これで問題は解決しました。しかし、それがどのように機能を宣言しているのか説明できますか? 'disable_interruption'はタイプです。 – Timesquare

+0

@ティムスクエア:それは興味深い問題です。あなたはそれについて読むことができます[ここ](http://stackoverflow.com/questions/1034606/is-there-any-use-for-local-function-declarations)。 –

+0

最も厄介な構文解析:) – Kratz

関連する問題