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

后端 未结 1 1196
臣服心动
臣服心动 2021-01-22 09:49

I am trying to make the following continuation work - but f.get() blocks. Whats wrong?

#include 

#define BOOST_THREAD_PROVIDES_FUTU         


        
1条回答
  •  执笔经年
    2021-01-22 10:31

    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 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.

    0 讨论(0)
提交回复
热议问题