ReactiveX / RxCpp

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

Integrating RxCpp into external event loop #471

Closed bobkocisko closed 5 years ago

bobkocisko commented 5 years ago

Thanks for this really awesome library! I'm evaluating RxCpp for inclusion into our application which is single threaded and based entirely on an epoll-based event loop (Linux-only). It's not clear to me if RxCpp can plug into this or if it has to take control of the event loop. I've been poking around to try to find examples but so far I don't see anything that is clearly pointing me in a direction. Also, is it possible for RxCpp to utilize just one thread or does it have to spawn multiple threads in the course of running? Multiple threads is not a deal-breaker for us but so far we have been able to stick with one thread and that's ideal for us due to the simplicity and lock-free aspects of single threaded programming.

BlueSolei commented 5 years ago

(I am not an expert on this, so please take my help here with a grain of salt)

1.RxCpp can work great with one thread. see how it has enabled in RxQT, see also the examples. specifically this one. you can see it gives the main_thread as a scheduler. This means that if you don't explicitly introduce a thread, there will be no threads in your Rx constructs. this example shows how to 'force' the subscription to be called on the 'main thread' when work is done (subscribe_on) a different thread. Your scenario doesn't need the work to be given to a different thread, but you still have two modes of Rx operators execution. The first mode is immediate. the Rx expression is evaluated on each item emitted from the observable. The second mode is that emotions are pushed one at a time into a queue, and executed FIFO at the end of the main event loop.

Once you declared RLP it become the default scheduler (cool feature, just found out) For the immediate mode, create Rx expressions and use the rxcpp::identity_immediate() scheduler. For the RLP mode, default scheduler will do. I created an example project here Please fill free to ask anything :-)

P.S. I found Rx extremely powerful but also have a very steep learning curve. not so much good documentation for RxCpp, and overall it's a big and sometimes very subtle subject. For RxCpp this the only book I found. and it doesn't do a great job :-( Another review of resources for rxcpp

bobkocisko commented 5 years ago

Thank you very much for your in-depth response! The Qt example looks like what I would need to do this. Not sure yet what direction I'll go in but this is a huge help.