Fix: constructing/assigning detached std::thread was terminating

This commit is contained in:
Galik 2017-04-04 04:24:16 +01:00
parent 01692e579e
commit b40739f743
3 changed files with 20 additions and 3 deletions

View File

@ -47,13 +47,13 @@ public:
detached_thread(detached_thread&& other): t(std::move(other.t)) {} detached_thread(detached_thread&& other): t(std::move(other.t)) {}
detached_thread(std::thread const&) = delete; 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 const&) = delete;
detached_thread& operator=(detached_thread&& other) noexcept { t = std::move(other.t); return *this; } 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 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<typename Callable, typename... Args> template<typename Callable, typename... Args>
explicit detached_thread(Callable&& f, Args&&... args) explicit detached_thread(Callable&& f, Args&&... args)

View File

@ -199,6 +199,23 @@ SUITE(detached_thread_tests)
CHECK(t1.get_id() == id1); CHECK(t1.get_id() == id1);
CHECK(t2.get_id() == id2); 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(); } int main() { return UnitTest::RunAllTests(); }

@ -1 +1 @@
Subproject commit dc6b90838014ab985bf3cd74ac17ad9d00e1fbcb Subproject commit 6d3cc12eab7e4e18c2058acccd467acea4bcd79b