mirror of
https://github.com/microsoft/GSL.git
synced 2024-11-03 17:56:43 -05:00
Fix: constructing/assigning detached std::thread was terminating
This commit is contained in:
parent
01692e579e
commit
b40739f743
@ -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)
|
||||||
|
@ -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
|
Loading…
Reference in New Issue
Block a user