オブザーバーが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 that
// s1's on_completed never got called.
私の質問は、いくつかの変数を設定してポーリングすることなく、s1 の on_completed が完了するまで待機する方法です。
observe_on() を使用する動機は、通常、値には複数のオブザーバーがあり、各オブザーバーを同時に実行したいからです。おそらく、同じ目標を達成するためのさまざまな方法があるでしょう。私はあなたのすべての提案を受け入れます.