ReactiveX / RxCpp

Reactive Extensions for C++
Apache License 2.0
3.05k stars 395 forks source link

Leaking threads? Is calling unsubscribe on worker required? #437

Closed Andersw88 closed 5 years ago

Andersw88 commented 6 years ago

When using create_worker with a make_new_thread the thread doesn't terminate when the worker goes out of scope. I would assume the thread should terminate when the worker goes out of scope and has no other subscriptions using it, but it seems you are required to call unsubscribe on the worker for the thread to terminate.

Example 1 crashes when its unable to create more threads while Example 2 loops indefinitely. Is this intended behavior?

Is it possible to create a worker with a thread that is alive until the worker goes out of scope and has no more subscribers?

Example 1

#include "rxcpp/rx.hpp"
#include "rxcpp/rx-scheduler.hpp"

int main()
{
    while (true) {
      auto sched = rxcpp::schedulers::make_new_thread();
      auto worker = sched.create_worker();
   }
}

Example 2

#include "rxcpp/rx.hpp"
#include "rxcpp/rx-scheduler.hpp"

int main()
{
    while (true) {
      auto sched = rxcpp::schedulers::make_new_thread();
      auto worker = sched.create_worker();
      worker.unsubscribe();
   }
}