Closed 0xffff00 closed 4 years ago
Hi,
By default rxcpp uses the current thread to generate the interval. That's why you saw the first 10 of outputs are always from task A, because the current thread is held inside the interval generator.
You can replace the code for the interval observable as follows to let the interval generation is handled by another thread instead of the current thread.
auto obe= rxcpp::observable<>::interval(200ms, rxcpp::observe_on_new_thread())
One of the differences between rxcpp and rxJava is the default scheduler. RxJava uses a thread pool as the default. Rxcpp uses a trampoline as the default. This means that the rxcpp version is using the main thread to emit all the intervals and that rxJava uses multiple threads in the pool to produce the intervals. To make rxcpp use different threads, use the same observe_on_new_thread() as a parameter to interval, this overrides the default.
There are other effects. The default in rxcpp also means that each subscribe completes emitting all the intervals before the next subscribe begins ( if you did the heavy tasks in a transform and then merged all the transforms into one subscribe you would get different results). I seem to remember a surprising behavior in interval that emits relative to now() at the time of construction.
Combining these effects results in all of A being emitted first and then B & C being interleaved.
I seem to remember a surprising behavior in interval that emits relative to now() at the time of construction.
Combining these effects results in all of A being emitted first and then B & C being interleaved.
Yes, I think it's the one reported here: https://github.com/ReactiveX/RxCpp/issues/413
Thanks! @nikobarli @kirkshoop
Here is my simple test for silmulate heavy blocking tasks. 4 threads ared used: 1 main thread holding the obserable and 3 observers regarded as paralleled tasks A,B,C respectively. the only obserable emit 10 times in every 200 ms. each task will sleep 180ms for silmutating heavy work payload. So, as expected, all jobs of 3 obervers should end up within ~2000ms. I have written simliar test cases both in RxCpp and RxJava:
observe_on(rxcpp::observe_on_new_thread())
vsobserveOn(Schedulers.newThread())
However, rxcpp used 3426ms. rxjava used 2036ms. Can anyone explain why? Do I misused rxcpp's API?
RxCpp TestCase
Code
Output
RxJava TestCase
Code
Output