boost threads return type operator()
I've been working with boost::threads to do some multithreading in my
code and have run into some questions that I haven't been able to find
answers to.
I'll include some sample code to illustrate my questions.
#include <boost/thread/thread.hpp>
#include <iostream>
using namespace std;
class mt
{
private:
double _alpha;
double _beta;
double _x;
double& _y1;
public:
double _y2;
public:
mt( double alpha, double beta, double x, double& y1 ):
_alpha(alpha), _beta(beta),
_x(x), _y1(y1) {};
void operator()()
{
// Some function more complicated than this...
_y1 = _alpha/_beta*_x;
_y2 = _y1;
};
double GetY2() { return _y2; };
};
void main()
{
double y1;
double y2;
mt a( 4,1,3,y1 );
boost::thread thrd( a );
thrd.join();
y2 = a.GetY2();
cout << y1 << endl;
cout << y2 << endl;
}
If I understand everything correctly, when I call
boost::thread thrd( a ),
it creates a new (thread local) instance of mt with which to work with
which goes out of scope at
thrd.join(). That is why y1 gives a good answer, but y2 contains garbage.
Now for my question:
Is it possible to call something like:
y = boost::thread thrd( a(x) );
where I've redefines operator() to take a double "x"
and return a double "y".
Then I don't have to give x and y when I construct "a".
Basically, my question is how to return something from the () operator
inside the thread.
Please ask for clarification if I haven't been clear.