ReactiveX / RxCpp

Reactive Extensions for C++
Apache License 2.0
3.03k stars 390 forks source link

Merge scheduler #604

Open czajah opened 7 months ago

czajah commented 7 months ago

Hi, I have few questions

1) Do I need to add scheduler to merge operator in this code? When do I have to provide it and why?

auto src1 = rxcpp::sources::interval(std::chrono::seconds(1), rxcpp::observe_on_new_thread())
        .map([](auto i){
            return "1# i: " + std::to_string(i);
        });

auto src2 = rxcpp::sources::interval(std::chrono::milliseconds(500), rxcpp::observe_on_new_thread())
        .map([](auto i){
            return "2# i: " + std::to_string(i);
        });

src1
.merge(src2)
.subscribe([](auto s){
    std::cout << s << std::endl;
});

2) Can I do something like this if I want to have only one thread instead of two (or three in case if I have to create anothed one for merge operator)?

auto sc = rxcpp::schedulers::make_new_thread();
auto w = sc.create_worker();
auto so = rxcpp::identity_same_worker(w);

auto src1 = rxcpp::sources::interval(std::chrono::seconds(1), so)
        .map([](auto i){
            return "1# i: " + std::to_string(i);
        });

auto src2 = rxcpp::sources::interval(std::chrono::milliseconds(500), so)
        .map([](auto i){
            return "2# i: " + std::to_string(i);
        });

src1
.merge(src2)
.subscribe([](auto s){
    std::cout << s << std::endl;
});