From 61450335a5f6621d25669f452d6fd45a057805e3 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Wed, 21 Apr 2021 17:19:53 +0300 Subject: [PATCH] named_thread: use concepts, improve constructors Better default thread name detection. --- Utilities/Thread.h | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/Utilities/Thread.h b/Utilities/Thread.h index e881d6081c..7360d7b2d6 100644 --- a/Utilities/Thread.h +++ b/Utilities/Thread.h @@ -42,7 +42,7 @@ class named_thread; class thread_base; -template +template struct result_storage { static constexpr bool empty = true; @@ -50,8 +50,8 @@ struct result_storage using type = void; }; -template -struct result_storage>>, Args...> +template requires (!std::is_void_v>) +struct result_storage { using T = std::invoke_result_t; @@ -84,6 +84,12 @@ struct result_storage +concept NamedThreadName = requires (T& t) +{ + std::string_view(t.thread_name); +}; + // Base class for task queue (linked list) class thread_future { @@ -110,12 +116,6 @@ public: } }; -template -struct thread_thread_name : std::bool_constant {}; - -template -struct thread_thread_name::thread_name)>> : std::bool_constant {}; - // Thread base class class thread_base { @@ -479,16 +479,17 @@ class named_thread final : public Context, result_storage, thread_base friend class thread_ctrl; public: - // Default constructor - named_thread() requires (std::is_default_constructible_v) && (thread_thread_name::value) - : Context() + // Forwarding constructor with default name (also potentially the default constructor) + template requires (std::is_constructible_v) && (NamedThreadName) + named_thread(Args&&... args) + : Context(std::forward(args)...) , thread(trampoline, Context::thread_name) { thread::start(); } // Normal forwarding constructor - template >> + template requires (std::is_constructible_v) && (!NamedThreadName) named_thread(std::string_view name, Args&&... args) : Context(std::forward(args)...) , thread(trampoline, name)