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