Ticket #3848 (thanks Nikki Chumakov) and (unrelated) exception_ptr refactoring.

[SVN r59364]
This commit is contained in:
Emil Dotchevski
2010-01-30 05:26:43 +00:00
parent 9dee0fb9e6
commit 4ae002cbf8
7 changed files with 224 additions and 159 deletions

View File

@ -24,7 +24,7 @@ run 2-throw_exception_no_exceptions_test.cpp ;
run 3-throw_exception_no_integration_test.cpp ;
run 4-throw_exception_no_both_test.cpp ;
run cloning_test.cpp ;
run copy_exception_test.cpp ;
run copy_exception_test.cpp /boost//thread : : : <threading>multi ;
run unknown_exception_test.cpp ;
run exception_test.cpp ;
run enable_error_info_test.cpp helper1.cpp ;

View File

@ -4,25 +4,106 @@
//file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <boost/exception_ptr.hpp>
#include <boost/exception/get_error_info.hpp>
#include <boost/thread.hpp>
#include <boost/detail/lightweight_test.hpp>
typedef boost::error_info<struct tag_answer,int> answer;
struct
test_exception:
std::exception
err:
virtual boost::exception,
virtual std::exception
{
};
int
main()
class
future
{
boost::exception_ptr p = boost::copy_exception(test_exception());
public:
future ():
ready_ (false)
{
}
void
set_exception( boost::exception_ptr const & e )
{
boost::unique_lock<boost::mutex> lck (mux_);
exc_ = e;
ready_ = true;
cond_.notify_all();
}
void
get_exception() const
{
boost::unique_lock<boost::mutex> lck (mux_);
while (! ready_)
cond_.wait (lck);
rethrow_exception (exc_);
}
private:
bool ready_;
boost::exception_ptr exc_;
mutable boost::mutex mux_;
mutable boost::condition_variable cond_;
};
void
producer( future & f )
{
f.set_exception (boost::copy_exception (err () << answer(42)));
}
void
consumer()
{
future f;
boost::thread thr (boost::bind (&producer, boost::ref (f)));
try
{
f.get_exception ();
}
catch(
err & e )
{
int const * ans=boost::get_error_info<answer>(e);
BOOST_TEST(ans && *ans==42);
}
thr.join();
}
void
consume()
{
for( int i=0; i!=100; ++i )
consumer();
}
void
thread_test()
{
boost::thread_group grp;
for( int i=0; i!=50; ++i )
grp.create_thread(&consume);
grp.join_all ();
}
void
simple_test()
{
boost::exception_ptr p = boost::copy_exception(err());
try
{
rethrow_exception(p);
BOOST_TEST(false);
}
catch(
test_exception & )
err & )
{
}
catch(
@ -30,5 +111,12 @@ main()
{
BOOST_TEST(false);
}
}
int
main()
{
simple_test();
thread_test();
return boost::report_errors();
}

View File

@ -18,91 +18,91 @@ void join( thread_handle & t );
class
thread_handle
{
thread_handle( thread_handle const & );
thread_handle & operator=( thread_handle const & );
{
thread_handle( thread_handle const & );
thread_handle & operator=( thread_handle const & );
boost::exception_ptr err_;
boost::thread t_;
boost::exception_ptr err_;
boost::thread t_;
static
void
thread_wrapper( boost::function<void()> const & f, boost::exception_ptr & ep )
{
BOOST_ASSERT(!ep);
try
{
f();
}
catch(...)
{
ep = boost::current_exception();
}
}
static
void
thread_wrapper( boost::function<void()> const & f, boost::exception_ptr & ep )
{
BOOST_ASSERT(!ep);
try
{
f();
}
catch(...)
{
ep = boost::current_exception();
}
}
explicit
thread_handle( boost::function<void()> const & f ):
t_(boost::bind(thread_wrapper,f,err_))
{
}
explicit
thread_handle( boost::function<void()> const & f ):
t_(boost::bind(thread_wrapper,f,err_))
{
}
friend boost::shared_ptr<thread_handle> create_thread( boost::function<void()> const & f );
friend void join( thread_handle & t );
};
friend boost::shared_ptr<thread_handle> create_thread( boost::function<void()> const & f );
friend void join( thread_handle & t );
};
boost::shared_ptr<thread_handle>
create_thread( boost::function<void()> const & f )
{
boost::shared_ptr<thread_handle> t( new thread_handle(f) );
return t;
}
{
boost::shared_ptr<thread_handle> t( new thread_handle(f) );
return t;
}
void
join( thread_handle & t )
{
t.t_.join();
if( t.err_ )
rethrow_exception(t.err_);
}
{
t.t_.join();
if( t.err_ )
rethrow_exception(t.err_);
}
struct exc: boost::exception, std::exception { };
typedef boost::error_info<struct answer_,int> answer;
void
thread_func()
{
BOOST_THROW_EXCEPTION(exc() << answer(42));
}
{
BOOST_THROW_EXCEPTION(exc() << answer(42));
}
void
check( boost::shared_ptr<thread_handle> const & t )
{
try
{
join(*t);
}
catch(
exc & e )
{
int const * a = boost::get_error_info<answer>(e);
BOOST_TEST(a && *a==42);
}
}
{
try
{
join(*t);
}
catch(
exc & e )
{
int const * a = boost::get_error_info<answer>(e);
BOOST_TEST(a && *a==42);
}
}
int
main()
{
try
{
std::vector< boost::shared_ptr<thread_handle> > threads;
std::generate_n(std::inserter(threads,threads.end()),256,boost::bind(create_thread,thread_func));
std::for_each(threads.begin(),threads.end(),check);
return boost::report_errors();
}
catch(
... )
{
BOOST_ERROR(boost::current_exception_diagnostic_information().c_str());
return 42;
}
try
{
std::vector< boost::shared_ptr<thread_handle> > threads;
std::generate_n(std::inserter(threads,threads.end()),256,boost::bind(create_thread,thread_func));
std::for_each(threads.begin(),threads.end(),check);
return boost::report_errors();
}
catch(
... )
{
BOOST_ERROR(boost::current_exception_diagnostic_information().c_str());
return 42;
}
}