Thursday, 12 September 2013

What is the correct way to return result by using c++11 thread?

What is the correct way to return result by using c++11 thread?

If I want to get the result from a thread, which of the following code is
correct? Or there exists better way to achieve the same goal?
void foo(int &result) {
result = 123;
}
int bar() {
return 123;
}
int main() {
int foo_result;
std::thread t1(foo, std::ref(foo_result));
t1.join();
std::future<int> t2 = std::async(bar);
int bar_result = t2.get();
}

No comments:

Post a Comment