Ticket #2309: BoostThreads.cpp

File BoostThreads.cpp, 964 bytes (added by rbock@…, 14 years ago)

Small sample, compile with g++ -fvisibility-hidden and link with the shared object instead of the static library

Line 
1#include <iostream>
2
3#include <boost/thread.hpp>
4
5using namespace std;
6
7boost::mutex mutex_;
8
9void perform()
10{
11 try
12 {
13 boost::this_thread::sleep(boost::posix_time::seconds(100));
14 }
15 catch(boost::thread_interrupted& interrupt)
16 {
17 boost::mutex::scoped_lock lock(mutex_);
18 cerr << "Thread " << boost::this_thread::get_id() << " got interrupted" << endl;
19 throw(interrupt);
20 }
21 catch(std::exception& e)
22 {
23 boost::mutex::scoped_lock lock(mutex_);
24 cerr << "Thread " << boost::this_thread::get_id() << " caught std::exception" << e.what() << endl;
25 }
26 catch(...)
27 {
28 boost::mutex::scoped_lock lock(mutex_);
29 cerr << "Thread " << boost::this_thread::get_id() << " caught something else" << endl;
30 }
31}
32
33
34
35
36int main()
37{
38 boost::thread_group threads;
39
40 for (int i = 0; i < 2; ++i)
41 {
42 threads.create_thread(perform);
43 }
44
45 ::sleep(1);
46 threads.interrupt_all();
47 threads.join_all();
48}
49