boost::thread

boost::thread を使ってスレッド起動を行ってみます。

boost::thread は環境依存の実装を吸収してくれるだけでなく使い勝手も良いです。特にメソッドを起動する際、_beginthreadを使用した場合は static なメソッドなどをいったん間にかませる必要があるのですが、これが不要で一発起動できて便利です。

 

 

 

1.引数無し関数をマルチスレッドで実行

 

評価環境:

 

#include <iostream>         // std::cout, std::endl
#include <boost/thread.hpp> // boost::thread

using namespace std;
//using namespace boost;

void PrintHello()
{ const size_t RepeatCount = 5 ; for ( size_t i=0; i<RepeatCount; ++i ){ cout << "Hello " << endl; Sleep(500); } } void PrintWorld() { const size_t RepeatCount = 5 ; for ( size_t i=0; i<RepeatCount; ++i ){ cout << "World! " << endl; Sleep(500); } } int main(int argc, char* argv[]) { // 引数無し関数をマルチスレッドで実行 { boost::thread thr_hello(&PrintHello); boost::thread thr_world(&PrintWorld); thr_hello.join(); thr_world.join(); } }

 

 

2.引数付き関数をマルチスレッドで実行

 

評価環境:

 

#include <iostream>         // std::cout, std::endl
#include <boost/thread.hpp> // boost::thread
#include <boost/bind.hpp>   // boost::bind

using namespace std;
//using namespace boost;

void PrintString( const char* str )
{
    const size_t RepeatCount = 5 ;
    for ( size_t i=0; i<RepeatCount; ++i ){
        cout << str << endl;
        Sleep(500);
    }
}


int main(int argc, char* argv[])
{
    // 引数付き関数をマルチスレッドで実行
    {
        boost::thread thr_hello(&PrintString, "Hello ");
        boost::thread thr_world(&PrintString, "World! ");
        boost::thread thr_space( bind(&PrintString, "Space ") );

        thr_hello.join();
        thr_world.join();
        thr_space.join();
    }
}

 

 

3.メンバ関数をマルチスレッドで実行(クラス外部から実行)

クラスメンバ関数をスレッド起動する方法です。クラスメソッドなので第一引数にインスタンス(this)が必要です。このため下記サンプルでは &hello, &world を引数で渡しています。

 

評価環境:

 

#include <iostream>         // std::cout, std::endl
#include <boost/thread.hpp> // boost::thread
#include <boost/bind.hpp>   // boost::bind

using namespace std;
//using namespace boost;

class PrintMessage {
private:
    const string strMessage_ ;

public:
    PrintMessage( string strMessage ) : strMessage_(strMessage)
    {
    }

    void run()
    {
        const size_t RepeatCount = 5 ;
        for ( size_t i=0; i<RepeatCount; ++i ){
            cout << strMessage_ << endl;
            Sleep(500);
        }
    }
};

int main(int argc, char* argv[])
{ // メンバ関数をマルチスレッドで実行(クラス外部からの起動) { PrintMessage hello("Hello "); PrintMessage world("World! "); boost::thread thr_hello( bind( &PrintMessage::run, &hello ) ); boost::thread thr_world( bind( &PrintMessage::run, &world ) ); thr_hello.join(); thr_world.join(); } }

 

 

4.メンバ関数をマルチスレッドで実行(クラス内部から実行)

   クラスメンバ関数をスレッド起動する方法です。クラスメソッドなので第一引数にインスタンス(this)が必要です。このため下記サンプルでは this を引数で渡しています。
  ここに記載の方法は私自身のオリジナルです。どこがオリジナルかというと、 swap で boost::thread のメンバ変数を交換しているところです。どこかから引用したわけではないので落とし穴があるかもしれません。逆にもっと美しい実現方法があるのかもしれません。まずは動作した、というだけの代物です。

 

評価環境:

#include <iostream>         // std::cout, std::endl
#include <boost/thread.hpp> // boost::thread
#include <boost/bind.hpp>   // boost::bind

using namespace std;
//using namespace boost;

class PrintMessage {
private:
    const string strMessage_ ;
    boost::thread thr_ ;

public:
    PrintMessage( string strMessage ) : strMessage_(strMessage)
    {
        boost::thread thr( bind(&PrintMessage::run, this) );
        thr_.swap( thr ) ;
    }

    void run()
    {
        const size_t RepeatCount = 5 ;
        for ( size_t i=0; i<RepeatCount; ++i ){
            cout << strMessage_ << endl;
            Sleep(500);
        }
    } 
    void join()
    {
        thr_.join(); 
    } 
};


int main(int argc, char* argv[])
{ // メンバ関数をマルチスレッドで実行(クラス内部からの起動) { PrintMessage hello("Hello "); PrintMessage world("World! "); hello.join(); world.join(); } }

 

 

ライセンス

本ページの情報は、特記無い限り下記 MIT ライセンスで提供されます。

The MIT License (MIT)

  Copyright 2010 Kinoshita Hidetoshi

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 

 

参考

 


 

変更履歴

2023-04-9 - ページデザイン更新
2012-03-04 - 説明など追記
2010-03-14 - 新規作成

 

Programming Items トップページ

プライバシーポリシー