Re: [Boost-users] [boost::thread] interrupt a future get boost::unknown_exception

64 views
Skip to first unread message

乔志强

unread,
Mar 20, 2011, 9:27:17 PM3/20/11
to boost...@lists.boost.org
It is only boost::thread

int foo()
{
this_thread::sleep(seconds(1000));
return 0;
}


int main(int argc, char** argv)
{
boost::packaged_task<int> pt(&foo);

boost::unique_future<int> fi = pt.get_future();
..........................

>
> ------------------------------
>
> Message: 2
> Date: Fri, 18 Mar 2011 16:43:54 +0100
> From: Viatchesla...@h-d-gmbh.de
> To: boost...@lists.boost.org
> Subject: Re: [Boost-users] [boost::thread] interrupt a future get
> boost::unknown_exception
> Message-ID: <op.vsjpzgww5q4lf0@pc-sysolts-zdmf>
> Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes
>
> On Fri, 18 Mar 2011 11:30:11 +0100, ??? <qiaozh...@leadcoretech.com>
> wrote:
>
> > [Windows XP, VC++ 10, boost 1.46]
> > When interrupt a future, the future.get() throw boost::unknown_exception,
> > Not boost::thread_interrupted.
> >
> > Class boost::thread_interrupted should have a base class for
> > current_exception_impl() to catch it ? or use BOOST_THROW_EXCEPTION in
> > interruptible_wait) ?
> >
> >
> > ///////////////
> > namespace boost
> > {
> > class thread_interrupted
> > {};
> > }
> >
> >
> > exception_ptr current_exception_impl()
> > {
> > try
> > {
> > throw;
> > }
> > catch(
> > exception_detail::clone_base & e )
> > {
> > return exception_ptr(e.clone());
> > }
> > ................
> > catch(
> > std::exception & e )
> > {
> > return
> > exception_detail::current_exception_unknown_std_exception(e);
> > }
> > catch(
> > boost::exception & e )
> > {
> > return
> > exception_detail::current_exception_unknown_boost_exception(e);
> > }
> > catch(
> > ... )
> > {
> > return
> > exception_detail::current_exception_unknown_exception();
> > }
> >
> >
> > // unknown_exception//////////////////
> > ERROR: Throw in function (unknown)
> > Dynamic exception type: class boost::exception_detail::clone_impl<class
> > boost::unknown_exception>
> > std::exception::what: Unknown exception
> >
> >
> >
> > //code /////////////////////
> > #include <boost/thread.hpp>
> > #include <boost/date_time/posix_time/posix_time_types.hpp>
> > #include <boost/thread/future.hpp>
> > using namespace boost::posix_time;
> > using namespace boost;
> >
> > int main(int argc, char** argv)
> > {
> > boost::packaged_task<int> pt(
> > [=]()->int
> > {
> > this_thread::sleep(seconds(1000));
> > return 0;
> > }
> > );
> > boost::unique_future<int> fi = pt.get_future();
> > boost::thread task(std::move(pt)); // launch task on a thread
> >
> > task.interrupt();
> >
> > try
> > {
> > int v = fi.get();
> > }
> > catch (boost::exception& exc)
> > {
> > std::cerr << "ERROR: " << boost::diagnostic_information(exc) <<
> > std::endl;
> > }
> > }
>
> Wow, boost-1.46 and lambdas, you're really working on the edge. Just to be
> sure it is a boost problem - can you make a classical function instead
> lambda and try with it? The test case seems nearly like one from the
> futures example - I really doubt it is a boost problem.
> I'd be glad to experiment with it yourself but my latest ubuntu has only
> gcc 4.4.5 and the lambdas said to be supported first from 4.5, plus I'd
> need to build 1.46 first, which takes quite a time on my nearly-netbook..
>
> -- Slava
>
>
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

乔志强

unread,
Mar 20, 2011, 10:46:29 PM3/20/11
to boost...@lists.boost.org
It may be a bug of the thread or future.

in
boost_1_46_0/doc/html/thread/synchronization.html

these Member functions:
shared_future class template:
Member function get()
Member function wait()
Member function timed_wait()
and
unique_future class template:
Member function get()
Member function wait()
Member function timed_wait()


Throws:
........
boost::thread_interrupted if the result associated with *this is not ready at the point of the call, and the [current] thread is interrupted.
............

the [current] thread is interrupted.
Should be
the [working] thread is interrupted ?

> > [Windows XP, VC++ 10, boost 1.46]
> > When interrupt a future, the future.get() throw boost::unknown_exception,
> > Not boost::thread_interrupted.
> >
> > Class boost::thread_interrupted should have a base class for

> > current_exception_impl() to catch it ? or use BOOST_THROW_EXCEPTION to throw ?
//////////

namespace boost
{

class thread_interrupted
{};
}

These function use [ throw thread_interrupted(); ] :
this_thread ::interruption_point() // pthread / win32
this_thread::interruptible_wait() // win32
interruption_checker:: check_for_interruption()// pthread
/////////////


// [Windows XP, VC++ 10, boost 1.46]


#include <boost/thread.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/thread/future.hpp>
using namespace boost::posix_time;
using namespace boost;

int foo()
{
this_thread::sleep(seconds(1000));
return 0;
}


int main(int argc, char** argv)
{
boost::packaged_task<int> pt(&foo);


boost::unique_future<int> fi = pt.get_future();

boost::thread task(std::move(pt)); // launch task on a thread

task.interrupt();

try
{
int v = fi.get();
}
catch (boost::exception& exc)
{
std::cerr << "ERROR: " << boost::diagnostic_information(exc) << std::endl;
}
}


> >


> > // unknown_exception//////////////////
> > ERROR: Throw in function (unknown)
> > Dynamic exception type: class boost::exception_detail::clone_impl<class
> > boost::unknown_exception>
> > std::exception::what: Unknown exception

Viatchesla...@h-d-gmbh.de

unread,
Mar 21, 2011, 12:37:20 PM3/21/11
to boost...@lists.boost.org
On Mon, 21 Mar 2011 02:27:17 +0100, 乔志强 <qiaozh...@leadcoretech.com>
wrote:

> It is only boost::thread
>
> int foo()
> {
> this_thread::sleep(seconds(1000));
> return 0;
> }
>
>
> int main(int argc, char** argv)
> {
> boost::packaged_task<int> pt(&foo);
>
> boost::unique_future<int> fi = pt.get_future();
> ..........................
>

The problem is reproducible with gcc 4.4.5 and boost 1.46.1 on ubuntu x86
linux. I'd classify the issue as a boost bug and submit it into tracker
unless somebody from boost::future knowers clarify the situation.

Viatchesla...@h-d-gmbh.de

unread,
Mar 21, 2011, 12:54:25 PM3/21/11
to boost...@lists.boost.org
On Mon, 21 Mar 2011 17:37:20 +0100, <Viatchesla...@h-d-gmbh.de>
wrote:

> On Mon, 21 Mar 2011 02:27:17 +0100, 乔志强
> <qiaozh...@leadcoretech.com> wrote:
>
>> It is only boost::thread
>>
>> int foo()
>> {
>> this_thread::sleep(seconds(1000));
>> return 0;
>> }
>>
>>
>> int main(int argc, char** argv)
>> {
>> boost::packaged_task<int> pt(&foo);
>>
>> boost::unique_future<int> fi = pt.get_future();
>> ..........................
>>
>
> The problem is reproducible with gcc 4.4.5 and boost 1.46.1 on ubuntu
> x86 linux. I'd classify the issue as a boost bug and submit it into
> tracker unless somebody from boost::future knowers clarify the situation.
>
> -- Slava
>

Just to clarify what I've got from little debugging session: the user task
is wrapped in try catch(...) clause, so when the future job throws, the
author's idea was likely to save the exception to simulate kind of rethrow
in future.get(). Unfortunately I do not know a mean to save an exception
in general, so seems the author stumbled upon it too, and currently the
exception being thrown in future is lost and general unknown_exception is
reported with get().

Reply all
Reply to author
Forward
0 new messages