From aae338a91c5de7e78d6b2280c008afdfd61838c7 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Sat, 28 Mar 2020 11:05:54 +0300 Subject: [PATCH] named_thread_group: add a default constructor --- Utilities/Thread.h | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/Utilities/Thread.h b/Utilities/Thread.h index 7e87c42126..40469f19d4 100644 --- a/Utilities/Thread.h +++ b/Utilities/Thread.h @@ -427,6 +427,11 @@ class named_thread_group final Thread* m_threads; + void init_threads() + { + m_threads = static_cast(::operator new(sizeof(Thread) * m_count, std::align_val_t{alignof(Thread)})); + } + public: // Lambda constructor, also the implicit deduction guide candidate named_thread_group(std::string_view name, u32 count, const Context& f) @@ -438,7 +443,7 @@ public: return; } - m_threads = static_cast(::operator new(sizeof(Thread) * m_count, std::align_val_t{alignof(Thread)})); + init_threads(); // Create all threads for (u32 i = 0; i < m_count; i++) @@ -447,6 +452,25 @@ public: } } + // Default constructor + named_thread_group(std::string_view name, u32 count) + : m_count(count) + , m_threads(nullptr) + { + if (count == 0) + { + return; + } + + init_threads(); + + // Create all threads + for (u32 i = 0; i < m_count; i++) + { + new (static_cast(m_threads + i)) Thread(std::string(name) + std::to_string(i + 1)); + } + } + named_thread_group(const named_thread_group&) = delete; named_thread_group& operator=(const named_thread_group&) = delete;