| 1 | #include <iostream>
 | 
|---|
| 2 | #include <exception>
 | 
|---|
| 3 | #include <stdexcept>
 | 
|---|
| 4 | #include <vector>
 | 
|---|
| 5 | 
 | 
|---|
| 6 | //#define USE_CPP11
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #ifdef __GXX_EXPERIMENTAL_CXX0X__
 | 
|---|
| 9 | #include <thread>
 | 
|---|
| 10 | #include <future>
 | 
|---|
| 11 | #include <memory>
 | 
|---|
| 12 | #else
 | 
|---|
| 13 | #include <boost/thread.hpp>
 | 
|---|
| 14 | #include <boost/shared_ptr.hpp>
 | 
|---|
| 15 | using namespace boost;
 | 
|---|
| 16 | #endif
 | 
|---|
| 17 | 
 | 
|---|
| 18 | using namespace std;
 | 
|---|
| 19 | 
 | 
|---|
| 20 | void executer(mutex& mtx, exception_ptr ex)
 | 
|---|
| 21 | {
 | 
|---|
| 22 |         for(int i = 0; i < 9999999; i++)
 | 
|---|
| 23 |         {
 | 
|---|
| 24 |                 try
 | 
|---|
| 25 |                 {
 | 
|---|
| 26 |                         lock_guard<mutex> lck(mtx);
 | 
|---|
| 27 |                         rethrow_exception(ex);
 | 
|---|
| 28 |                 }
 | 
|---|
| 29 |                 catch(...)
 | 
|---|
| 30 |                 {
 | 
|---|
| 31 |                         current_exception(); // <- run concurrently
 | 
|---|
| 32 |                 }
 | 
|---|
| 33 |         }
 | 
|---|
| 34 | }
 | 
|---|
| 35 | void test()
 | 
|---|
| 36 | {
 | 
|---|
| 37 |         exception_ptr ex;
 | 
|---|
| 38 | 
 | 
|---|
| 39 |         try
 | 
|---|
| 40 |         {
 | 
|---|
| 41 |                 throw runtime_error("O_o");
 | 
|---|
| 42 |         }
 | 
|---|
| 43 |         catch(...)
 | 
|---|
| 44 |         {
 | 
|---|
| 45 |                 // use current_exception() it's compulsory condition
 | 
|---|
| 46 |                 // because only in this case will be used unsafe detail::refcount_ptr
 | 
|---|
| 47 |                 // (in current_exception_std_exception_wrapper inherited from exception)
 | 
|---|
| 48 |                 ex = current_exception();
 | 
|---|
| 49 |         }
 | 
|---|
| 50 | 
 | 
|---|
| 51 |         mutex mtx;
 | 
|---|
| 52 |         vector<shared_ptr<thread> > group;
 | 
|---|
| 53 |         for(int i = 0; i < 10; i++)
 | 
|---|
| 54 |                 group.push_back(shared_ptr<thread>(new thread(bind(executer, ref(mtx), ex))));
 | 
|---|
| 55 | 
 | 
|---|
| 56 |         vector<shared_ptr<thread> >::iterator it = group.begin();
 | 
|---|
| 57 |         for(; it != group.end(); it++)
 | 
|---|
| 58 |             (*it)->join();
 | 
|---|
| 59 | }
 | 
|---|
| 60 | 
 | 
|---|
| 61 | void version()
 | 
|---|
| 62 | {
 | 
|---|
| 63 | #ifdef __GXX_EXPERIMENTAL_CXX0X__
 | 
|---|
| 64 |     cout << "use c++11 implementation\n";
 | 
|---|
| 65 | #else
 | 
|---|
| 66 |     cout << "use boost implementation\n";
 | 
|---|
| 67 | #endif
 | 
|---|
| 68 | }
 | 
|---|
| 69 | 
 | 
|---|
| 70 | int main()
 | 
|---|
| 71 | {
 | 
|---|
| 72 |     version();
 | 
|---|
| 73 |         cout << "Start\n";
 | 
|---|
| 74 | 
 | 
|---|
| 75 |         test();
 | 
|---|
| 76 | 
 | 
|---|
| 77 |         cout << "Exit\n";
 | 
|---|
| 78 | }
 | 
|---|