私は「C++ 標準ライブラリを超えて」という本を持っていますが、ブーストを使用したマルチスレッドの例はありません。ブーストを使用して2つのスレッドが実行される簡単な例を誰かが私に見せてくれるほど親切でしょうか?
28264 次
1 に答える
38
これは私の最小限のBoostスレッドの例です。
#include <boost/thread.hpp>
#include <iostream>
using namespace std;
void ThreadFunction()
{
int counter = 0;
for(;;)
{
cout << "thread iteration " << ++counter << " Press Enter to stop" << endl;
try
{
// Sleep and check for interrupt.
// To check for interrupt without sleep,
// use boost::this_thread::interruption_point()
// which also throws boost::thread_interrupted
boost::this_thread::sleep(boost::posix_time::milliseconds(500));
}
catch(boost::thread_interrupted&)
{
cout << "Thread is stopped" << endl;
return;
}
}
}
int main()
{
// Start thread
boost::thread t(&ThreadFunction);
// Wait for Enter
char ch;
cin.get(ch);
// Ask thread to stop
t.interrupt();
// Join - wait when thread actually exits
t.join();
cout << "main: thread ended" << endl;
return 0;
}
于 2012-09-15T12:49:44.217 に答える