은 ...사용 멤버 함수 :: packaged_task
내가하고 싶은 모두에서 클래스의 멤버 함수를 시작하는 것입니다 배경 특정 시간에 . 그 기능의 결과는 또한 "외부 적으로"이용 가능해야한다. 그래서 생성자에서 작업을 준비하고 (미래 변수 설정, ...) 나중에 나중에 시작하고 싶습니다.
나는 표준 결합을 시도 : | 비동기 | :(packaged_task 미래를)하지만 나는 그것이 작동하지 않았다.이 조각은 컴파일되지 않습니다,하지만 난 그게 내가하고 싶은 것을 보여줍니다 생각 :
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(); }
};
사전에 감사!
'std :: async'는 형식이 아닌 함수입니다. –