From 525fdc181bcce48ef577a76a906b6615e3777096 Mon Sep 17 00:00:00 2001 From: Arendelle Date: Sun, 2 Nov 2025 09:44:31 +0800 Subject: [PATCH 1/2] default constructor for nt/win32 thread - add example for fast_io::thread --- examples/0040.thread/thread.cc | 36 +++++++++++++ include/fast_io_hosted/threads/thread/nt.h | 22 +++----- include/fast_io_hosted/threads/thread/win32.h | 53 ++++++++++++------- 3 files changed, 75 insertions(+), 36 deletions(-) create mode 100644 examples/0040.thread/thread.cc diff --git a/examples/0040.thread/thread.cc b/examples/0040.thread/thread.cc new file mode 100644 index 00000000..f6887588 --- /dev/null +++ b/examples/0040.thread/thread.cc @@ -0,0 +1,36 @@ +#include +#include + +int main() +{ +#ifdef _WIN32 // temporaryly disable this example on rest platforms + auto t = ::fast_io::thread{[](int param) +#if __cpp_static_call_operator >= 2020207L + static +#endif + noexcept { + ::fast_io::println("the param is: ", param); +#ifdef _WIN32 +#ifdef _WIN32_WINDOWS + ::fast_io::println("the child thread id is: ", ::fast_io::this_thread::get_id()); +#else + ::fast_io::println("the child thread id is: ", ::fast_io::mnp::pointervw(::fast_io::this_thread::get_id())); +#endif +#endif + // ::fflush(stdout); + ::fast_io::this_thread::sleep_for(::std::chrono::seconds{1}); + }, + 5}; + + t.join(); +#ifdef _WIN32 +#ifdef _WIN32_WINDOWS + ::fast_io::println("the parent thread id is: ", ::fast_io::this_thread::get_id()); +#else + ::fast_io::println("the parent thread id is: ", ::fast_io::mnp::pointervw(::fast_io::this_thread::get_id())); +#endif +#endif + + return 0; +#endif +} diff --git a/include/fast_io_hosted/threads/thread/nt.h b/include/fast_io_hosted/threads/thread/nt.h index 0e99dbd1..39f71d82 100644 --- a/include/fast_io_hosted/threads/thread/nt.h +++ b/include/fast_io_hosted/threads/thread/nt.h @@ -66,13 +66,11 @@ class nt_thread using native_handle_type = void *; private: - id id_; - native_handle_type handle_; + id id_{nullptr}; + native_handle_type handle_{nullptr}; public: - nt_thread() noexcept - : id_{nullptr}, handle_{nullptr} - {} + constexpr nt_thread() noexcept = default; template requires(::std::invocable) @@ -115,12 +113,7 @@ class nt_thread constexpr nt_thread(nt_thread const &) noexcept = delete; - constexpr nt_thread(nt_thread &&other) noexcept - : id_(other.id_), handle_(other.handle_) - { - other.id_ = nullptr; - other.handle_ = nullptr; - } + constexpr nt_thread(nt_thread &&other) noexcept = default; constexpr ~nt_thread() noexcept { @@ -189,17 +182,14 @@ class nt_thread ::std::ranges::swap(id_, other.id_); } - /** - * @note Unlike std::thread::get_id, this method return the const reference. - */ [[nodiscard]] - constexpr auto &&get_id() const noexcept + constexpr auto get_id() const noexcept { return this->id_; } [[nodiscard]] - constexpr auto &&native_handle() const noexcept + constexpr auto native_handle() const noexcept { return this->handle_; } diff --git a/include/fast_io_hosted/threads/thread/win32.h b/include/fast_io_hosted/threads/thread/win32.h index 68440c3b..b53cac8a 100644 --- a/include/fast_io_hosted/threads/thread/win32.h +++ b/include/fast_io_hosted/threads/thread/win32.h @@ -65,13 +65,11 @@ class win32_thread using native_handle_type = void *; private: - id id_; - native_handle_type handle_; + id id_{}; + native_handle_type handle_{nullptr}; public: - win32_thread() noexcept - : id_{}, handle_{nullptr} - {} + win32_thread() noexcept = default; template requires(::std::invocable) @@ -104,12 +102,7 @@ class win32_thread constexpr win32_thread(win32_thread const &) noexcept = delete; - constexpr win32_thread(win32_thread &&other) noexcept - : id_(other.id_), handle_(other.handle_) - { - other.id_ = 0; - other.handle_ = nullptr; - } + constexpr win32_thread(win32_thread &&other) noexcept = default; constexpr ~win32_thread() noexcept { @@ -142,7 +135,12 @@ class win32_thread return this->id_ != 0; } - constexpr void join() +#if __cpp_constexpr >= 202207L + // https://en.cppreference.com/w/cpp/compiler_support/23.html#cpp_constexpr_202207L + // for reduce some warning purpose + constexpr +#endif + void join() { if (!this->joinable()) [[unlikely]] { @@ -152,7 +150,12 @@ class win32_thread this->id_ = 0; } - constexpr void detach() +#if __cpp_constexpr >= 202207L + // https://en.cppreference.com/w/cpp/compiler_support/23.html#cpp_constexpr_202207L + // for reduce some warning purpose + constexpr +#endif + void detach() { if (!this->joinable()) [[unlikely]] { @@ -172,22 +175,26 @@ class win32_thread ::std::ranges::swap(id_, other.id_); } - /** - * @note Unlike std::thread::get_id, this method return the const reference. - */ [[nodiscard]] - constexpr auto &&get_id() const noexcept + constexpr auto get_id() const noexcept { return this->id_; } [[nodiscard]] - constexpr auto &&native_handle() const noexcept + constexpr auto native_handle() const noexcept { return this->handle_; } - static constexpr ::std::uint_least32_t hardware_concurrency() noexcept + [[nodiscard]] + static +#if __cpp_constexpr >= 202207L + // https://en.cppreference.com/w/cpp/compiler_support/23.html#cpp_constexpr_202207L + // for reduce some warning purpose + constexpr +#endif + ::std::uint_least32_t hardware_concurrency() noexcept { ::fast_io::win32::system_info si{}; ::fast_io::win32::GetSystemInfo(__builtin_addressof(si)); @@ -198,7 +205,13 @@ class win32_thread namespace this_thread { -constexpr auto get_id() noexcept -> ::fast_io::win32::win32_thread::id +[[nodiscard]] +#if __cpp_constexpr >= 202207L +// https://en.cppreference.com/w/cpp/compiler_support/23.html#cpp_constexpr_202207L +// for reduce some warning purpose +constexpr +#endif + auto get_id() noexcept -> ::fast_io::win32::win32_thread::id { return ::fast_io::win32::GetCurrentThreadId(); } From 1449bab7d6dc7727fb2cbc1c145857f171dd7a0f Mon Sep 17 00:00:00 2001 From: Arendelle Date: Sun, 2 Nov 2025 15:26:29 +0800 Subject: [PATCH 2/2] default move construct to thread head_guard --- include/fast_io_hosted/threads/thread/c_malloc_guard.h | 6 +----- include/fast_io_hosted/threads/thread/nt.h | 6 +----- include/fast_io_hosted/threads/thread/win32.h | 6 +----- 3 files changed, 3 insertions(+), 15 deletions(-) diff --git a/include/fast_io_hosted/threads/thread/c_malloc_guard.h b/include/fast_io_hosted/threads/thread/c_malloc_guard.h index bd17f646..85d48006 100644 --- a/include/fast_io_hosted/threads/thread/c_malloc_guard.h +++ b/include/fast_io_hosted/threads/thread/c_malloc_guard.h @@ -13,11 +13,7 @@ class thread_start_routine_tuple_c_malloc_allocate_guard {} constexpr thread_start_routine_tuple_c_malloc_allocate_guard(thread_start_routine_tuple_c_malloc_allocate_guard const &) noexcept = delete; - constexpr thread_start_routine_tuple_c_malloc_allocate_guard(thread_start_routine_tuple_c_malloc_allocate_guard &&other) noexcept - : ptr_{other.ptr_} - { - other.ptr_ = nullptr; - } + constexpr thread_start_routine_tuple_c_malloc_allocate_guard(thread_start_routine_tuple_c_malloc_allocate_guard &&other) noexcept = default; constexpr ~thread_start_routine_tuple_c_malloc_allocate_guard() { diff --git a/include/fast_io_hosted/threads/thread/nt.h b/include/fast_io_hosted/threads/thread/nt.h index 39f71d82..ffe9a10a 100644 --- a/include/fast_io_hosted/threads/thread/nt.h +++ b/include/fast_io_hosted/threads/thread/nt.h @@ -26,11 +26,7 @@ class nt_thread_start_routine_tuple_allocate_guard {} constexpr nt_thread_start_routine_tuple_allocate_guard(nt_thread_start_routine_tuple_allocate_guard const &) noexcept = delete; - constexpr nt_thread_start_routine_tuple_allocate_guard(nt_thread_start_routine_tuple_allocate_guard &&other) noexcept - : ptr_{other.ptr_} - { - other.ptr_ = nullptr; - } + constexpr nt_thread_start_routine_tuple_allocate_guard(nt_thread_start_routine_tuple_allocate_guard &&other) noexcept = default; constexpr ~nt_thread_start_routine_tuple_allocate_guard() { diff --git a/include/fast_io_hosted/threads/thread/win32.h b/include/fast_io_hosted/threads/thread/win32.h index b53cac8a..4498d897 100644 --- a/include/fast_io_hosted/threads/thread/win32.h +++ b/include/fast_io_hosted/threads/thread/win32.h @@ -26,11 +26,7 @@ class win32_thread_start_routine_tuple_allocate_guard {} constexpr win32_thread_start_routine_tuple_allocate_guard(win32_thread_start_routine_tuple_allocate_guard const &) noexcept = delete; - constexpr win32_thread_start_routine_tuple_allocate_guard(win32_thread_start_routine_tuple_allocate_guard &&other) noexcept - : ptr_{other.ptr_} - { - other.ptr_ = nullptr; - } + constexpr win32_thread_start_routine_tuple_allocate_guard(win32_thread_start_routine_tuple_allocate_guard &&other) noexcept = default; constexpr ~win32_thread_start_routine_tuple_allocate_guard() {