小编LMC*_*LMC的帖子

RxCpp:观察者的生命周期,如果使用observe_on(rxcpp::observe_on_new_thread())

如果观察者使用observe_on(rxcpp::observe_on_new_thread()),等待所有观察者on_completed被调用的正确方法是什么:

例如:

{
    Foo foo;
    auto generator = [&](rxcpp::subscriber<int> s)
    {
        s.on_next(1);
        // ...
        s.on_completed();
    };
    auto values = rxcpp::observable<>::create<int>(generator).publish();
    auto s1 = values.observe_on(rxcpp::observe_on_new_thread())
                    .subscribe([&](int) { slow_function(foo); }));

    auto lifetime = rxcpp::composite_subscription();
    lifetime.add([&](){ wrapper.log("unsubscribe");  });
    auto s2 = values.ref_count().as_blocking().subscribe(lifetime);

    // hope to call something here to wait for the completion of
    // s1's on_completed function
}

// the program usually crashes here when foo goes out of scope because 
// the slow_function(foo) is still working on foo.  I also noticed …
Run Code Online (Sandbox Code Playgroud)

c++ reactive-programming rxcpp

5
推荐指数
1
解决办法
1749
查看次数

标签 统计

c++ ×1

reactive-programming ×1

rxcpp ×1