/* This program demonstrates a simple usage of futures. * The main program creates (via the async() call) two threads that do some computation. * Then, the main thread waits for their results, adds them together and prints the result. */ #include #include #include #include #include #include #include #include #include #include template class Future { public: virtual ~Future() {} T get() const { std::unique_lock lck(m_mutex); m_cv.wait(lck, [this]() {return m_isSet; }); return m_val; } void set(T val) { std::unique_lock lck(m_mutex); m_val = val; m_isSet = true; m_cv.notify_all(); } private: bool m_isSet = false; T m_val; mutable std::mutex m_mutex; mutable std::condition_variable m_cv; }; template class AsyncTask : public Future::type> { public: explicit AsyncTask(Func f) :m_func(f), m_thread([this]() {threadFunc(); }) { // nothing } ~AsyncTask() override { m_thread.join(); } private: void threadFunc() { this->set(m_func()); } Func m_func; std::thread m_thread; }; template std::shared_ptr::type> > launchAsync(Func f) { return std::make_shared >(f); } std::shared_ptr > produce(int a, int b) { return launchAsync([a, b]() {return a + b; }); } int main(int argc, char** argv) { std::shared_ptr > f1 = produce(10, 15); std::shared_ptr > f2 = produce(15, 20); int r = f1->get() + f2->get(); printf("Result=%d\n", r); }