ReactiveX / RxCpp

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

new_worker std::thread detach always throws system_error? #516

Open iam opened 4 years ago

iam commented 4 years ago

Is this behavior intentional?

Calling std::thread::detach in the new_worker constructor will always throw std::system_error https://github.com/ReactiveX/RxCpp/blob/master/Rx/v2/src/rxcpp/schedulers/rx-newthread.hpp#L84

            if (keepAlive->worker.joinable() && keepAlive->worker.get_id() != std::this_thread::get_id()) {
                guard.unlock();
                keepAlive->worker.join();
            }
            else {
                keepAlive->worker.detach();   // always throws system_error
            }

The above detach could get executed when joinable returns false, but according to https://en.cppreference.com/w/cpp/thread/thread/detach if joinable returns false then system_error is always thrown.

Should lines 83-85 be changed to add an if joinable?

            else if (keepAlive->worker.joinable()) {
                keepAlive->worker.detach();
            }