What I want to do should be quite easy, but I don't get it...
All I want to do is to start a member function of a class in background at some certain point in time. The result of that function should also be "externally" available. So I want to prepare the task in the constructor (setting the future variable, ... ) and start it at some later point.
I tried to combine std::(packaged_task|async|future) but I didn't get it to work.
This snippet will not compile, but I think it shows what I want to do:
class foo {
private:
// This function shall run in background as a thread
// when it gets triggered to start at some certain point
bool do_something() { return true; }
std::packaged_task<bool()> task;
std::future<bool> result;
public:
foo() :
task(do_something), // yes, that's wrong, but how to do it right?
result(task.get_future())
{
// do some initialization stuff
.....
}
~foo() {}
void start() {
// Start Task as asynchron thread
std::async as(std::launch::async, task); // Also doesn't work...
}
// This function should return the result of do_something
bool get_result() { return result.get(); }
};
Thanks in advance!