| 1 | #include <iostream>
|
|---|
| 2 |
|
|---|
| 3 | #include <boost/thread.hpp>
|
|---|
| 4 |
|
|---|
| 5 | using namespace std;
|
|---|
| 6 |
|
|---|
| 7 | boost::mutex mutex_;
|
|---|
| 8 |
|
|---|
| 9 | void 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 |
|
|---|
| 36 | int 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 |
|
|---|