From 01692e579ec4e33a165996d58d78da8fee379f6e Mon Sep 17 00:00:00 2001 From: Galik Date: Mon, 3 Apr 2017 05:56:15 +0100 Subject: [PATCH] Added gsl_thread.h (raii_thread & detached_thread) --- include/gsl/gsl_thread | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/include/gsl/gsl_thread b/include/gsl/gsl_thread index d63989f..afe9ad5 100644 --- a/include/gsl/gsl_thread +++ b/include/gsl/gsl_thread @@ -38,17 +38,47 @@ namespace gsl class detached_thread { + friend void swap(detached_thread& t1, detached_thread& t2) noexcept; + public: + detached_thread() noexcept = default; + detached_thread(detached_thread const&) = delete; + 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& 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; } template explicit detached_thread(Callable&& f, Args&&... args) : t(std::forward(f), std::forward(args)...) { t.detach(); } + bool joinable() const { return t.joinable(); } + + std::thread::id get_id() const noexcept { return t.get_id(); } + + std::thread::native_handle_type native_handle() { return t.native_handle(); } + + void join() { t.join(); } + + void swap(detached_thread& other) noexcept { using std::swap; swap(t, other.t); } + private: std::thread t; }; +void swap(detached_thread& t1, detached_thread& t2) noexcept +{ + using std::swap; + swap(t1.t, t2.t); +} + class raii_thread { friend void swap(raii_thread& t1, raii_thread& t2) noexcept;