Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.36 KB | None | 0 0
  1. namespace Thread{
  2. class CThreadManagerTest{
  3. public:
  4. // コンストラクタ1. 関数を引き受けて起動. bool bDelay が true なら遅延起動.
  5. CThreadManagerTest( boost::function<void (void)> func, int threadID = 1, bool bDelay = false ) : m_func(func), m_threadID(threadID), m_bDelay(bDelay){
  6. if( !m_bDelay ){
  7. this->m_th = boost::thread( func );
  8. this->m_bAlive = true;
  9. }else{
  10. this->m_bAlive = false;
  11. }
  12. }
  13.  
  14. // コンストラクタ2. 関数を引き受けないタイプ. 関数の指定がないので 遅延起動.
  15. CThreadManagerTest( int threadID = 1 ) : m_func(NULL), m_threadID(threadID), m_bDelay(false){
  16. this->m_bAlive = false;
  17. }
  18.  
  19. // デストラクタ.
  20. ~CThreadManagerTest(){
  21. if( this->m_th.joinable() ){
  22. this->m_th.join();
  23. }
  24. }
  25.  
  26. // Runメンバ関数. 遅延起動時 効果あり.
  27. void Run( void ){
  28. if( this->m_bDelay && !this->m_func.empty() ){
  29. this->m_th = boost::thread( m_func );
  30. }
  31. }
  32.  
  33. // SetThreadメンバ関数. スレッド設定.
  34. void SetThread( boost::function<void (void)> func ){
  35. if( !this->m_bAlive ){
  36. this->m_th = boost::thread( func );
  37. }
  38. }
  39.  
  40. // Detachメンバ関数. スレッド放棄.
  41. void Detach( void ) throw(CExceptionEx){
  42. try{
  43. if( this->m_bAlive ) this->m_th.detach();
  44. }catch( std::system_error &e ){
  45. throw CExceptionEx( string("スレッド(ID: ") + IntToString(m_threadID) + string(")譲渡不可"), -(m_threadID) );
  46. }
  47. }
  48.  
  49. // Joinメンバ関数. 別スレッド終了まで待機.
  50. void Join( void ) throw(CExceptionEx){
  51. try{
  52.  
  53. }catch( boost::thread_interrupted &e ){
  54. throw CExceptionEx( string("スレッド(ID: ") + IntToString(m_threadID) + string(")は中断されたため待機不可"), -(m_threadID) );
  55. }catch( std::system_error &e ){
  56. throw CExceptionEx( string("スレッド(ID: ") + IntToString(m_threadID) + string(")は中断されたため待機不可"), -(m_threadID) );
  57. }
  58. }
  59.  
  60. // Joinableメンバ関数
  61. bool Joinable( void ){
  62. return this->m_th.joinable();
  63. }
  64.  
  65. // Interruptメンバ関数. 別スレッドに"終了"を指令. 別スレッドは InterruptPoint非メンバ関数でチェックする.
  66. void Interrupt( void ){
  67. if( this->m_bAlive ) m_th.interrupt();
  68. }
  69.  
  70. // InterruptPoint非メンバ関数. 別スレッドから受けた"終了" 命令を受け取り、自スレッドを終了させる.
  71. static void InterruptPoint( void ){
  72. boost::this_thread::interruption_point();
  73. }
  74.  
  75. // Sleep非メンバ関数. 自スレッドを指定時間 休む.
  76. template<class chrono_duration>
  77. static void Sleep( const chrono_duration &sleeptime ){
  78. boost::this_thread::sleep_for( sleeptime );
  79. }
  80.  
  81. // Sleep非メンバ関数. 自スレッドを指定時間 休む.
  82. static void Sleep( int milisec ){
  83. boost::this_thread::sleep_for( boost::chrono::milliseconds( milisec ) );
  84. }
  85. private:
  86. boost::function<void (void)> m_func;
  87. boost::thread m_th;
  88. bool m_bDelay;
  89. bool m_bAlive;
  90. bool m_threadID;
  91. };
  92.  
  93. // 動かしたいスレッドがあるクラス
  94. class CThreadTest{
  95. public:
  96. CThreadTest( int id ) : m_id(id), m_a(1){}
  97. ~CThreadTest(){}
  98.  
  99. // 関数オブジェクトにするために オーバーロード.
  100. void operator()(){ this->Main(); }
  101.  
  102. // スレッドのエントリーポイント
  103. void Main( void ){
  104. for( int i = 0; i < 1000; i++ ){
  105. CThreadManagerTest::Sleep( 1000 );
  106. CThreadManagerTest::InterruptPoint(); // スレッドを止めるかどうか
  107.  
  108. //mtx.Enter();
  109. m_a++;
  110. // mtx.Leave();
  111. cout << m_a << endl;
  112. }
  113. }
  114. private:
  115. CMutex mtx;
  116. int m_id;
  117. int m_a;
  118. };
  119.  
  120. /*
  121. namespace: Thread
  122.  
  123. class : ---
  124.  
  125. function : func
  126.  
  127. arguments:
  128. void
  129.  
  130. return : void
  131.  
  132. quotation:
  133.  
  134. note :
  135. */
  136. void func( void ){
  137. /*
  138. // 方法1: コンストラクタ1 で生成してみる
  139. {
  140. CThreadTest Thread( 100 ); // 使用したい関数オブジェクト
  141. CThreadManagerTest Th( Thread ); // コンストラクタ1 で生成
  142. CThreadManagerTest::Sleep( 4000 ); // mainスレッドを1秒間止める
  143. Th.Interrupt(); // スレッドを止める
  144. }
  145. */
  146.  
  147. // 方法2: コンストラクタ2 で生成してみる
  148. {
  149. CThreadTest Thread( 100 ); // 使用したい関数オブジェクト
  150. CThreadManagerTest Th( Thread, 100, true ); // コンストラクタ2 で生成
  151. Th.Run(); // スレッドをセット
  152. //CThreadManagerTest::Sleep( 4000 ); // mainスレッドを1秒間止める
  153. int a = 10;
  154. Th.Interrupt(); // スレッドを止める
  155. }
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement