From b40739f743395123da0fef841227b2bc18a155da Mon Sep 17 00:00:00 2001 From: Galik Date: Tue, 4 Apr 2017 04:24:16 +0100 Subject: [PATCH] Fix: constructing/assigning detached std::thread was terminating --- include/gsl/gsl_thread | 4 ++-- tests/thread_tests.cpp | 17 +++++++++++++++++ tests/unittest-cpp | 2 +- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/include/gsl/gsl_thread b/include/gsl/gsl_thread index afe9ad5..f3f760a 100644 --- a/include/gsl/gsl_thread +++ b/include/gsl/gsl_thread @@ -47,13 +47,13 @@ public: detached_thread(detached_thread&& other): t(std::move(other.t)) {} detached_thread(std::thread const&) = delete; - detached_thread(std::thread&& other) noexcept: t(std::move(other)) { t.detach(); } + detached_thread(std::thread&& other) noexcept: t(std::move(other)) { if(t.joinable()) t.detach(); } detached_thread& operator=(detached_thread const&) = delete; detached_thread& operator=(detached_thread&& other) noexcept { t = std::move(other.t); return *this; } detached_thread& operator=(std::thread const&) = delete; - detached_thread& operator=(std::thread&& other) noexcept { t = std::move(other); t.detach(); return *this; } + detached_thread& operator=(std::thread&& other) noexcept { t = std::move(other); if(t.joinable()) t.detach(); return *this; } template explicit detached_thread(Callable&& f, Args&&... args) diff --git a/tests/thread_tests.cpp b/tests/thread_tests.cpp index 3b85df0..01f1db3 100644 --- a/tests/thread_tests.cpp +++ b/tests/thread_tests.cpp @@ -199,6 +199,23 @@ SUITE(detached_thread_tests) CHECK(t1.get_id() == id1); CHECK(t2.get_id() == id2); } + + TEST(detached_thread_ctor_from_detached_std_thread) + { + std::thread t{[&]{ sleep_for(t_100ms); }}; + t.detach(); + gsl::detached_thread{std::move(t)}; + sleep_for(t_100ms * 2); + } + + TEST(detached_thread_assign_from_detached_std_thread) + { + std::thread t1{[&]{ sleep_for(t_100ms); }}; + t1.detach(); + gsl::detached_thread t2; + t2 = std::move(t1); + sleep_for(t_100ms * 2); + } } int main() { return UnitTest::RunAllTests(); } diff --git a/tests/unittest-cpp b/tests/unittest-cpp index dc6b908..6d3cc12 160000 --- a/tests/unittest-cpp +++ b/tests/unittest-cpp @@ -1 +1 @@ -Subproject commit dc6b90838014ab985bf3cd74ac17ad9d00e1fbcb +Subproject commit 6d3cc12eab7e4e18c2058acccd467acea4bcd79b