I'm experimenting with std::future, but haven't found any materials that deal with the usecase I have in mind and was hoping someone could either confirm or deny the validity of the approach excerpted below:
Code:
class Thing {
private:
std::string mValue;
public:
Thing() : mValue( "" ) {}
void set(const std::string& s)
{
mValue = s;
reverse( mValue.begin(), mValue.end() );
}
const std::string& getValue() const
{
return mValue;
}
};
int main(int argc, const char * argv[])
{
Thing* tA = new Thing();
Thing* tB = new Thing();
Thing* tC = new Thing();
auto tLambda = [](const string& iStr, Thing* oThing) -> Thing* {
oThing->set( iStr );
return oThing;
};
future<Thing*> tA1 = async( launch::async, tLambda, "This is a test...A", tA );
future<Thing*> tB1 = async( launch::async, tLambda, "This is a test...B", tB );
future<Thing*> tC1 = async( launch::async, tLambda, "This is a test...C", tC );
future<Thing*> tA2 = async( launch::async, tLambda, tA1.get()->getValue(), tA );
future<Thing*> tB2 = async( launch::async, tLambda, tB1.get()->getValue(), tB );
future<Thing*> tC2 = async( launch::async, tLambda, tC1.get()->getValue(), tC );
cout << tA2.get()->getValue() << endl;
cout << tB2.get()->getValue() << endl;
cout << tC2.get()->getValue() << endl;
return 0;
}
In essence, I'd like to use std::future to update a pre-existing object's member variables rather than initializing and returning a new object (as is shown in most std::future examples I've seen). In the example above, the model seems a little suspect. I would love to hear any thoughts on the best way to achieve this sort of functionality. Ultimately, I would like to embed this in a thread-pool sort of context.
Thanks!