Topic: How to safely use threads, futures and async?
Author: =?ISO-8859-2?Q?Andrzej_Krzemie=F1ski?= <akrzemi1@gmail.com>
Date: Wed, 22 Jun 2011 08:23:16 CST Raw View
Hi,
I recently saw Hans Boehm's presentation on threads at Boostcon, and
the following question started to bother me. It was decided that in C+
+ when you are destroying a joinable thread std::terminate will be
called. This document:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2802.html
describes the design rationale. We cannot detach upon destruction
because this may lead to accessing invalid memory locations, and we
also cannot just join because it could cause "some potentially tricky
performance debugging problems". Thus the following code is in some
sense a design error because if function g throws an exception we will
terminate the program.
int compute( int x, int y ) {
int fx = 0;
thread t{ [=, &fx]{ fx = f(x); } };
int gy = g(y); // will throw
t.join();
return fx + gy;
}
My question is, how this situation is different if I use async()
instead:
int compute( int x, int y ) {
auto fx = async( [=]{ return f(x); } );
int gy = g(y); // will throw
return fx.get() + gy;
}
If g() throws and I do not call get(), there obviously exists a thread
under the covers that is still executing. What will happen when future
fx is destroyed? Will we detach from or join with the other thread?
Regards,
&rzej
--
[ comp.std.c++ is moderated. To submit articles, try posting with your ]
[ newsreader. If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Anthony Williams <anthony.ajw@gmail.com>
Date: Thu, 23 Jun 2011 13:43:08 CST Raw View
On Jun 22, 3:23 pm, Andrzej Krzemie ski <akrze...@gmail.com> wrote:
> Thus the following code is in some
> sense a design error because if function g throws an exception we will
> terminate the program.
>
> int compute( int x, int y ) {
> int fx = 0;
> thread t{ [=, &fx]{ fx = f(x); } };
> int gy = g(y); // will throw
> t.join();
> return fx + gy;
> }
>
> My question is, how this situation is different if I use async()
> instead:
>
> int compute( int x, int y ) {
> auto fx = async( [=]{ return f(x); } );
> int gy = g(y); // will throw
> return fx.get() + gy;
> }
>
> If g() throws and I do not call get(), there obviously exists a thread
> under the covers that is still executing. What will happen when future
> fx is destroyed? Will we detach from or join with the other thread?
If get() or wait() has not been called, the destructor of the last
future referencing the shared state will wait for the async thread to
complete. In this case there is only one future (fx), so compute will
not return until the async thread has completed.
Anthony
--
Author of C++ Concurrency in Action http://www.stdthread.co.uk/book/
just::thread C++0x thread library http://www.stdthread.co.uk
Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk
15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No.
5478976
--
[ comp.std.c++ is moderated. To submit articles, try posting with your ]
[ newsreader. If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]