Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Simple return value from std::thread?

With win32 threads I have the straight forward GetExitCodeThread() that gives me the value which the thread function returned. I'm looking for something similar for std::thread (or boost threads)
As I understand this can be done with futures but how exactly?

like image 230
shoosh Avatar asked Oct 07 '11 12:10

shoosh


People also ask

Can a thread function return a value in C?

If you want to return only status of the thread (say whether the thread completed what it intended to do) then just use pthread_exit or use a return statement to return the value from the thread function.

How do you return a value from a thread?

How to Return Values From a Thread. A thread cannot return values directly. The start() method on a thread calls the run() method of the thread that executes our code in a new thread of execution. The run() method in turn may call a target function, if configured.

How does a thread return a value in CPP?

#include <thread> #include <future> int func() { return 1; } std::future<int> ret = std::async(&func); int i = ret. get();

What is a future C++?

A future is an object that can retrieve a value from some provider object or function, properly synchronizing this access if in different threads. "Valid" futures are future objects associated to a shared state, and are constructed by calling one of the following functions: async.


3 Answers

See this video tutorial on C++11 futures.

Explicitly with threads and futures:

#include <thread>
#include <future>

void func(std::promise<int> && p) {
    p.set_value(1);
}

std::promise<int> p;
auto f = p.get_future();
std::thread t(&func, std::move(p));
t.join();
int i = f.get();

Or with std::async (higher-level wrapper for threads and futures):

#include <thread>
#include <future>
int func() { return 1; }
std::future<int> ret = std::async(&func);
int i = ret.get();

I can't comment whether it works on all platforms (it seems to work on Linux, but doesn't build for me on Mac OSX with GCC 4.6.1).

like image 158
Alex B Avatar answered Oct 07 '22 22:10

Alex B


I'd say:

#include <thread>
#include <future>

int simplefunc(std::string a)
{ 
    return a.size();
}

int main()
{
      auto future = std::async(simplefunc, "hello world");
      int simple = future.get();

      return simple;
}

Note that async even propagates any exceptions thrown from the thread function

like image 44
sehe Avatar answered Oct 08 '22 00:10

sehe


Using C++11 threads, one can't get the return value as thread exit which used to be the case with pthread_exit(...)

You need to use C++11 Future<> to get the return value. Future is created using templated argument where the template takes the return value (built in of User Defined types)..

You can fetch the value in another thread using future<..>::get(..) function.

one benefit of using future<..> is that you can check the validity of return value i.e if it's already taken, you avoid calling get() accidentally by checking the validity using future<..>::isValid(...) function.

Here is how you'll write the code.

#include <iostream>
#include <future>
using namespace std;
auto retFn() {
    return 100;
}
int main() {
    future<int> fp = async(launch::async, retFn);
    if(fp.valid())
       cout<<"Return value from async thread is => "<<fp.get()<<endl;
    return 0;
}

it should also be noted that we can get the future run on the same thread by using launch::deferred option as

 future<int> fp = async(launch::deferred, retFn);
like image 4
Deepak Kr Gupta Avatar answered Oct 07 '22 22:10

Deepak Kr Gupta