Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions examples/0040.thread/thread.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <chrono>
#include <fast_io.h>

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
}
6 changes: 1 addition & 5 deletions include/fast_io_hosted/threads/thread/c_malloc_guard.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
28 changes: 7 additions & 21 deletions include/fast_io_hosted/threads/thread/nt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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 <typename Func, typename... Args>
requires(::std::invocable<Func, Args...>)
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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_;
}
Expand Down
59 changes: 34 additions & 25 deletions include/fast_io_hosted/threads/thread/win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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 <typename Func, typename... Args>
requires(::std::invocable<Func, Args...>)
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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]]
{
Expand All @@ -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]]
{
Expand All @@ -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));
Expand All @@ -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();
}
Expand Down