How to get return value from a function called which executes in another thread in TBB?

Nabijon picture Nabijon · Oct 13, 2012 · Viewed 8.2k times · Source

In the code:

    #include <tbb/tbb.h>

    int GetSomething()
    {
        int something;
        // do something
        return something;
    }

    // ...
    tbb::tbb_thread(GetSomething, NULL);
    // ...

Here GetSomething() was called in another thread via its pointer. But can we get return value from GetSomething()? How?

Answer

twid picture twid · Oct 13, 2012

You can use pass by reference to get value from thread

#include <tbb/tbb.h>

void GetSomething(int *result)
{

    result= // do something
}

// ...
int result;
tbb::tbb_thread(GetSomething, &result);
tbb.join();
//use result as you like