boost::future and continuations - value set, but future still blocks

霸气de小男生 提交于 2019-12-02 02:08:52

The problem is, your composed future is not kept around. In fact, it is a temporary and it gets destructed as soon as the statement (with .then()) ends.

Fix it:

int main () {
    Foo foo;

    auto f1 = foo.start();
    auto f2 = f1.then([](boost::future<int> f) {
        std::cout << "done:" << std::endl;
        std::cout << f.get() << std::endl;
    });

    foo.finish();
    f2.get();
}

Now it prints

done:
23

See it Live On Coliru

If you move the f2.get() before the foo.finish() it will dead lock again.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!