「boost::thread 2つのスレッドを動かすシンプルな例」の版間の差分

提供: C++入門
移動: 案内検索
(ページの作成:「<!-- vim: filetype=mediawiki --> __TOC__ == 概要 == boost threadで2つのスレッドだけを生成するシンプルな例です。 == boost_thread_2.cpp...」)
 
行44: 行44:
 
         return 0;
 
         return 0;
 
}
 
}
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
行57: 行56:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
% ./boost_thread_2
 
% ./boost_thread_2
void thread_1()void thread_2()
+
void thread_1()
 
+
void thread_2()
 
void thread_2()
 
void thread_2()
 
void thread_1()
 
void thread_1()
行67: 行66:
 
void thread_1()
 
void thread_1()
 
void thread_2()
 
void thread_2()
 
 
</syntaxhighlight>
 
</syntaxhighlight>
 
  
 
== 関連項目 ==
 
== 関連項目 ==

2013年3月10日 (日) 12:47時点における版


概要

boost threadで2つのスレッドだけを生成するシンプルな例です。

boost_thread_2.cpp の例

ソースコード boost_thread_2.cpp

#include <boost/thread.hpp>
#include <iostream>
#define MAX 5
 
void
thread_1 () {
        for (int i = 0; i < MAX; i++) {
                std::cout << __PRETTY_FUNCTION__ << std::endl;
                boost::this_thread::sleep(boost::posix_time::milliseconds(100));
        }
}
void
thread_2 () {
        for (int i = 0; i < MAX; i++) {
                std::cout << __PRETTY_FUNCTION__ << std::endl;
                boost::this_thread::sleep(boost::posix_time::milliseconds(100));
        }
}
 
int
main(int argc, char const* argv[])
{
        boost::thread th1(thread_1);
        boost::thread th2(thread_2);
 
        th1.join ();
        th2.join ();
 
        return 0;
}

繧?ンパイル

g++ -I/usr/local/include -L/usr/local/lib -Lboost_thread boost_thread_2.cpp -o boost_thread_2

実行例

% ./boost_thread_2
void thread_1()
void thread_2()
void thread_2()
void thread_1()
void thread_2()
void thread_1()
void thread_1()
void thread_2()
void thread_1()
void thread_2()

関連項目