Re: [Boost-users] How to retrieve a value from a thread
I was though thread is created by a pointer to a function, e.g. Int foo(int x, int y, int z) { // do something with a,b,c return a+b+c; } Boost::thread pop(boost::bind(&foo, a, b, c)); // do something else pop.join(); The question is how to retrieve the result from foo running in pop. Thanks for ideas. Kind regards Pshemek Przemyslaw Sliwa Global Foreign Exchange FX Risk Engine Development Merrill Lynch ( +44 (0)20 7995 4182 ) +44 (0)79 6325 2319 * Email: przemyslaw_sliwa@ml.com -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Nat Goodspeed Sent: 09 March 2006 15:15 To: boost-users@lists.boost.org Subject: Re: [Boost-users] How to retrieve a value from a thread ________________________________________ From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Sliwa, Przemyslaw (London) Sent: Thursday, March 09, 2006 9:47 AM To: boost-users@lists.boost.org Subject: [Boost-users] How to retrieve a value from a thread I would like to know if it is possible to retrieve a value returned from a function which is called by a thread. [Nat] Is this thread running continuously, doing other things? Or did you start it for the purpose of calling that function, so that when the function is done the thread terminates? In Java, I've always kind of liked Doug Lea's FutureResult idiom: http://gee.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/Future Result.html I don't know of anything similar in the existing library. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users -------------------------------------------------------- If you are not an intended recipient of this e-mail, please notify the sender, delete it and do not read, act upon, print, disclose, copy, retain or redistribute it. Click here for important additional terms relating to this e-mail. http://www.ml.com/email_terms/ --------------------------------------------------------
Try the following inline changes. Sliwa, Przemyslaw (London) wrote:
I was though thread is created by a pointer to a function, e.g.
void AssignResult( Int& aRef, Int aVal ) { aRef = aVal; }
Int foo(int x, int y, int z) { // do something with a,b,c return a+b+c; }
Int lResult = 0;
Boost::thread pop( boost::bind( &AssignResult , boost::ref( lResult ) , boost::bind(&foo, a, b, c) ) );
// do something else
pop.join();
assert( lResult = foo( a, b, c ) ); is one possibilty. This has not been tested. The inner bind may need to be 'protected', see http://www.boost.org/libs/bind/bind.html#nested_binds. Jeff Flinn
Sliwa, Przemyslaw (London) escreveu:
I was though thread is created by a pointer to a function, e.g.
Int foo(int x, int y, int z) { // do something with a,b,c return a+b+c; }
Boost::thread pop(boost::bind(&foo, a, b, c)); // do something else
pop.join();
The question is how to retrieve the result from foo running in pop.
void foo_wrapper (int x, int y, int z, int& out) { out = foo(x, y, z); } int retval = 0; boost::thread pop( boost::bind(&foo_wrapper, a, b, c, retval) ); pop.join(); // Check retval. -- Pedro Lamarão
participants (3)
-
Jeff Flinn
-
Pedro Lamarão
-
Sliwa, Przemyslaw (London)