summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIgor Murashkin <iam@google.com>2019-11-12 13:49:00 -0800
committerandroid-build-merger <android-build-merger@google.com>2019-11-12 13:49:00 -0800
commit1222f0344b28cef8d7d75215ac1b3084c31b54dd (patch)
tree48190a0eabae4551974acd747b5d59a466cd66d9
parentfc1046c15d9830f24db7d5b20420a7bf884c1a4a (diff)
parenta7ed673121a1594551fa0dfbece4b065a2a56d80 (diff)
downloadRxCpp-1222f0344b28cef8d7d75215ac1b3084c31b54dd.tar.gz
schedulers: Fix std::thread clean-up race in newthread am: 96616d3ccb am: b33827d11a
am: a7ed673121 Change-Id: Id414a136f660dcf882a045ae79da53212a73b77d
-rw-r--r--Rx/v2/src/rxcpp/schedulers/rx-newthread.hpp18
1 files changed, 11 insertions, 7 deletions
diff --git a/Rx/v2/src/rxcpp/schedulers/rx-newthread.hpp b/Rx/v2/src/rxcpp/schedulers/rx-newthread.hpp
index 5145e92..7c93469 100644
--- a/Rx/v2/src/rxcpp/schedulers/rx-newthread.hpp
+++ b/Rx/v2/src/rxcpp/schedulers/rx-newthread.hpp
@@ -37,6 +37,16 @@ private:
virtual ~new_worker_state()
{
+ // Ensure that std::thread is no longer joinable,
+ // otherwise the destructor will call std::terminate.
+ if (!worker.joinable()) {
+ return;
+ }
+ if (worker.get_id() != std::this_thread::get_id()) {
+ worker.join();
+ } else {
+ worker.detach();
+ }
}
explicit new_worker_state(composite_subscription cs)
@@ -76,13 +86,7 @@ private:
if (!keepAlive->q.empty()) std::terminate();
keepAlive->wake.notify_one();
- if (keepAlive->worker.joinable() && keepAlive->worker.get_id() != std::this_thread::get_id()) {
- guard.unlock();
- keepAlive->worker.join();
- }
- else {
- keepAlive->worker.detach();
- }
+ // ~new_worker_state cleans up the std::thread
});
state->worker = tf([keepAlive](){