int xxx()
{
return 5;
}
int main()
{
boost::thread th;
th = boost::thread(xxx);
th.join();
return 0;
}
How to catch the value returned by the xxx() method without the use of boost::promise?
Since you say you can't change xxx
, call another function which puts the result somewhere accessible. A promise is probably still the best option, but you could write it directly to a local variable if you're careful with synchronisation. For example
int result;
th = boost::thread([&]{result = xxx();});
// Careful! result will be modified by the thread.
// Don't access it without synchronisation.
th.join();
// result is now safe to access.
As mentioned in the comments, there's a handy async
function which gives you a future from which to retrieve an asynchronously-called function's return value:
auto future = boost::async(boost::launch::async, xxx);
int result = future.get();