1 //Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc.
3 //Distributed under the Boost Software License, Version 1.0. (See accompanying
4 //file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #include <boost/exception_ptr.hpp>
7 #include <boost/exception/info.hpp>
8 #include <boost/exception/get_error_info.hpp>
9 #include <boost/exception/diagnostic_information.hpp>
10 #include <boost/function.hpp>
11 #include <boost/bind.hpp>
12 #include <boost/thread.hpp>
13 #include <boost/detail/atomic_count.hpp>
14 #include <boost/detail/lightweight_test.hpp>
18 boost::shared_ptr<thread_handle> create_thread( boost::function<void()> const & f );
19 void join( thread_handle & t );
24 thread_handle( thread_handle const & );
25 thread_handle & operator=( thread_handle const & );
27 boost::exception_ptr err_;
32 thread_wrapper( boost::function<void()> const & f, boost::exception_ptr & ep )
41 ep = boost::current_exception();
46 thread_handle( boost::function<void()> const & f ):
47 t_(boost::bind(thread_wrapper,f,err_))
51 friend boost::shared_ptr<thread_handle> create_thread( boost::function<void()> const & f );
52 friend void join( thread_handle & t );
55 boost::shared_ptr<thread_handle>
56 create_thread( boost::function<void()> const & f )
58 boost::shared_ptr<thread_handle> t( new thread_handle(f) );
63 join( thread_handle & t )
67 rethrow_exception(t.err_);
70 boost::detail::atomic_count exc_count(0);
74 virtual boost::exception,
75 virtual std::exception
95 exc & operator=( exc const & );
98 typedef boost::error_info<struct answer_,int> answer;
103 BOOST_THROW_EXCEPTION(exc() << answer(42));
107 check( boost::shared_ptr<thread_handle> const & t )
116 int const * a = boost::get_error_info<answer>(e);
117 BOOST_TEST(a && *a==42);
124 BOOST_TEST(++exc_count==1);
127 std::vector< boost::shared_ptr<thread_handle> > threads;
128 std::generate_n(std::inserter(threads,threads.end()),256,boost::bind(create_thread,thread_func));
129 std::for_each(threads.begin(),threads.end(),check);
130 return boost::report_errors();
136 "Caught unexpected exception.\n"
137 "Output from current_exception_diagnostic_information:\n" <<
138 boost::current_exception_diagnostic_information() << std::endl;
141 BOOST_TEST(!--exc_count);