Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Thread{
- class CThreadManagerTest{
- public:
- // コンストラクタ1. 関数を引き受けて起動. bool bDelay が true なら遅延起動.
- CThreadManagerTest( boost::function<void (void)> func, int threadID = 1, bool bDelay = false ) : m_func(func), m_threadID(threadID), m_bDelay(bDelay){
- if( !m_bDelay ){
- this->m_th = boost::thread( func );
- this->m_bAlive = true;
- }else{
- this->m_bAlive = false;
- }
- }
- // コンストラクタ2. 関数を引き受けないタイプ. 関数の指定がないので 遅延起動.
- CThreadManagerTest( int threadID = 1 ) : m_func(NULL), m_threadID(threadID), m_bDelay(false){
- this->m_bAlive = false;
- }
- // デストラクタ.
- ~CThreadManagerTest(){
- if( this->m_th.joinable() ){
- this->m_th.join();
- }
- }
- // Runメンバ関数. 遅延起動時 効果あり.
- void Run( void ){
- if( this->m_bDelay && !this->m_func.empty() ){
- this->m_th = boost::thread( m_func );
- }
- }
- // SetThreadメンバ関数. スレッド設定.
- void SetThread( boost::function<void (void)> func ){
- if( !this->m_bAlive ){
- this->m_th = boost::thread( func );
- }
- }
- // Detachメンバ関数. スレッド放棄.
- void Detach( void ) throw(CExceptionEx){
- try{
- if( this->m_bAlive ) this->m_th.detach();
- }catch( std::system_error &e ){
- throw CExceptionEx( string("スレッド(ID: ") + IntToString(m_threadID) + string(")譲渡不可"), -(m_threadID) );
- }
- }
- // Joinメンバ関数. 別スレッド終了まで待機.
- void Join( void ) throw(CExceptionEx){
- try{
- }catch( boost::thread_interrupted &e ){
- throw CExceptionEx( string("スレッド(ID: ") + IntToString(m_threadID) + string(")は中断されたため待機不可"), -(m_threadID) );
- }catch( std::system_error &e ){
- throw CExceptionEx( string("スレッド(ID: ") + IntToString(m_threadID) + string(")は中断されたため待機不可"), -(m_threadID) );
- }
- }
- // Joinableメンバ関数
- bool Joinable( void ){
- return this->m_th.joinable();
- }
- // Interruptメンバ関数. 別スレッドに"終了"を指令. 別スレッドは InterruptPoint非メンバ関数でチェックする.
- void Interrupt( void ){
- if( this->m_bAlive ) m_th.interrupt();
- }
- // InterruptPoint非メンバ関数. 別スレッドから受けた"終了" 命令を受け取り、自スレッドを終了させる.
- static void InterruptPoint( void ){
- boost::this_thread::interruption_point();
- }
- // Sleep非メンバ関数. 自スレッドを指定時間 休む.
- template<class chrono_duration>
- static void Sleep( const chrono_duration &sleeptime ){
- boost::this_thread::sleep_for( sleeptime );
- }
- // Sleep非メンバ関数. 自スレッドを指定時間 休む.
- static void Sleep( int milisec ){
- boost::this_thread::sleep_for( boost::chrono::milliseconds( milisec ) );
- }
- private:
- boost::function<void (void)> m_func;
- boost::thread m_th;
- bool m_bDelay;
- bool m_bAlive;
- bool m_threadID;
- };
- // 動かしたいスレッドがあるクラス
- class CThreadTest{
- public:
- CThreadTest( int id ) : m_id(id), m_a(1){}
- ~CThreadTest(){}
- // 関数オブジェクトにするために オーバーロード.
- void operator()(){ this->Main(); }
- // スレッドのエントリーポイント
- void Main( void ){
- for( int i = 0; i < 1000; i++ ){
- CThreadManagerTest::Sleep( 1000 );
- CThreadManagerTest::InterruptPoint(); // スレッドを止めるかどうか
- //mtx.Enter();
- m_a++;
- // mtx.Leave();
- cout << m_a << endl;
- }
- }
- private:
- CMutex mtx;
- int m_id;
- int m_a;
- };
- /*
- namespace: Thread
- class : ---
- function : func
- arguments:
- void
- return : void
- quotation:
- note :
- */
- void func( void ){
- /*
- // 方法1: コンストラクタ1 で生成してみる
- {
- CThreadTest Thread( 100 ); // 使用したい関数オブジェクト
- CThreadManagerTest Th( Thread ); // コンストラクタ1 で生成
- CThreadManagerTest::Sleep( 4000 ); // mainスレッドを1秒間止める
- Th.Interrupt(); // スレッドを止める
- }
- */
- // 方法2: コンストラクタ2 で生成してみる
- {
- CThreadTest Thread( 100 ); // 使用したい関数オブジェクト
- CThreadManagerTest Th( Thread, 100, true ); // コンストラクタ2 で生成
- Th.Run(); // スレッドをセット
- //CThreadManagerTest::Sleep( 4000 ); // mainスレッドを1秒間止める
- int a = 10;
- Th.Interrupt(); // スレッドを止める
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement