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/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 0e99dbd1..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() { @@ -66,13 +62,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 +109,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 +178,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..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() { @@ -65,13 +61,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 +98,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 +131,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 +146,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 +171,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 +201,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(); }