Skip to content

Commit 10ad14c

Browse files
Fix winthread (#1210)
* default constructor for nt/win32 thread - add example for fast_io::thread * default move construct to thread head_guard
1 parent 3ce234a commit 10ad14c

File tree

4 files changed

+78
-51
lines changed

4 files changed

+78
-51
lines changed

examples/0040.thread/thread.cc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <chrono>
2+
#include <fast_io.h>
3+
4+
int main()
5+
{
6+
#ifdef _WIN32 // temporaryly disable this example on rest platforms
7+
auto t = ::fast_io::thread{[](int param)
8+
#if __cpp_static_call_operator >= 2020207L
9+
static
10+
#endif
11+
noexcept {
12+
::fast_io::println("the param is: ", param);
13+
#ifdef _WIN32
14+
#ifdef _WIN32_WINDOWS
15+
::fast_io::println("the child thread id is: ", ::fast_io::this_thread::get_id());
16+
#else
17+
::fast_io::println("the child thread id is: ", ::fast_io::mnp::pointervw(::fast_io::this_thread::get_id()));
18+
#endif
19+
#endif
20+
// ::fflush(stdout);
21+
::fast_io::this_thread::sleep_for(::std::chrono::seconds{1});
22+
},
23+
5};
24+
25+
t.join();
26+
#ifdef _WIN32
27+
#ifdef _WIN32_WINDOWS
28+
::fast_io::println("the parent thread id is: ", ::fast_io::this_thread::get_id());
29+
#else
30+
::fast_io::println("the parent thread id is: ", ::fast_io::mnp::pointervw(::fast_io::this_thread::get_id()));
31+
#endif
32+
#endif
33+
34+
return 0;
35+
#endif
36+
}

include/fast_io_hosted/threads/thread/c_malloc_guard.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ class thread_start_routine_tuple_c_malloc_allocate_guard
1313
{}
1414

1515
constexpr thread_start_routine_tuple_c_malloc_allocate_guard(thread_start_routine_tuple_c_malloc_allocate_guard const &) noexcept = delete;
16-
constexpr thread_start_routine_tuple_c_malloc_allocate_guard(thread_start_routine_tuple_c_malloc_allocate_guard &&other) noexcept
17-
: ptr_{other.ptr_}
18-
{
19-
other.ptr_ = nullptr;
20-
}
16+
constexpr thread_start_routine_tuple_c_malloc_allocate_guard(thread_start_routine_tuple_c_malloc_allocate_guard &&other) noexcept = default;
2117

2218
constexpr ~thread_start_routine_tuple_c_malloc_allocate_guard()
2319
{

include/fast_io_hosted/threads/thread/nt.h

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ class nt_thread_start_routine_tuple_allocate_guard
2626
{}
2727

2828
constexpr nt_thread_start_routine_tuple_allocate_guard(nt_thread_start_routine_tuple_allocate_guard const &) noexcept = delete;
29-
constexpr nt_thread_start_routine_tuple_allocate_guard(nt_thread_start_routine_tuple_allocate_guard &&other) noexcept
30-
: ptr_{other.ptr_}
31-
{
32-
other.ptr_ = nullptr;
33-
}
29+
constexpr nt_thread_start_routine_tuple_allocate_guard(nt_thread_start_routine_tuple_allocate_guard &&other) noexcept = default;
3430

3531
constexpr ~nt_thread_start_routine_tuple_allocate_guard()
3632
{
@@ -66,13 +62,11 @@ class nt_thread
6662
using native_handle_type = void *;
6763

6864
private:
69-
id id_;
70-
native_handle_type handle_;
65+
id id_{nullptr};
66+
native_handle_type handle_{nullptr};
7167

7268
public:
73-
nt_thread() noexcept
74-
: id_{nullptr}, handle_{nullptr}
75-
{}
69+
constexpr nt_thread() noexcept = default;
7670

7771
template <typename Func, typename... Args>
7872
requires(::std::invocable<Func, Args...>)
@@ -115,12 +109,7 @@ class nt_thread
115109

116110
constexpr nt_thread(nt_thread const &) noexcept = delete;
117111

118-
constexpr nt_thread(nt_thread &&other) noexcept
119-
: id_(other.id_), handle_(other.handle_)
120-
{
121-
other.id_ = nullptr;
122-
other.handle_ = nullptr;
123-
}
112+
constexpr nt_thread(nt_thread &&other) noexcept = default;
124113

125114
constexpr ~nt_thread() noexcept
126115
{
@@ -189,17 +178,14 @@ class nt_thread
189178
::std::ranges::swap(id_, other.id_);
190179
}
191180

192-
/**
193-
* @note Unlike std::thread::get_id, this method return the const reference.
194-
*/
195181
[[nodiscard]]
196-
constexpr auto &&get_id() const noexcept
182+
constexpr auto get_id() const noexcept
197183
{
198184
return this->id_;
199185
}
200186

201187
[[nodiscard]]
202-
constexpr auto &&native_handle() const noexcept
188+
constexpr auto native_handle() const noexcept
203189
{
204190
return this->handle_;
205191
}

include/fast_io_hosted/threads/thread/win32.h

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ class win32_thread_start_routine_tuple_allocate_guard
2626
{}
2727

2828
constexpr win32_thread_start_routine_tuple_allocate_guard(win32_thread_start_routine_tuple_allocate_guard const &) noexcept = delete;
29-
constexpr win32_thread_start_routine_tuple_allocate_guard(win32_thread_start_routine_tuple_allocate_guard &&other) noexcept
30-
: ptr_{other.ptr_}
31-
{
32-
other.ptr_ = nullptr;
33-
}
29+
constexpr win32_thread_start_routine_tuple_allocate_guard(win32_thread_start_routine_tuple_allocate_guard &&other) noexcept = default;
3430

3531
constexpr ~win32_thread_start_routine_tuple_allocate_guard()
3632
{
@@ -65,13 +61,11 @@ class win32_thread
6561
using native_handle_type = void *;
6662

6763
private:
68-
id id_;
69-
native_handle_type handle_;
64+
id id_{};
65+
native_handle_type handle_{nullptr};
7066

7167
public:
72-
win32_thread() noexcept
73-
: id_{}, handle_{nullptr}
74-
{}
68+
win32_thread() noexcept = default;
7569

7670
template <typename Func, typename... Args>
7771
requires(::std::invocable<Func, Args...>)
@@ -104,12 +98,7 @@ class win32_thread
10498

10599
constexpr win32_thread(win32_thread const &) noexcept = delete;
106100

107-
constexpr win32_thread(win32_thread &&other) noexcept
108-
: id_(other.id_), handle_(other.handle_)
109-
{
110-
other.id_ = 0;
111-
other.handle_ = nullptr;
112-
}
101+
constexpr win32_thread(win32_thread &&other) noexcept = default;
113102

114103
constexpr ~win32_thread() noexcept
115104
{
@@ -142,7 +131,12 @@ class win32_thread
142131
return this->id_ != 0;
143132
}
144133

145-
constexpr void join()
134+
#if __cpp_constexpr >= 202207L
135+
// https://en.cppreference.com/w/cpp/compiler_support/23.html#cpp_constexpr_202207L
136+
// for reduce some warning purpose
137+
constexpr
138+
#endif
139+
void join()
146140
{
147141
if (!this->joinable()) [[unlikely]]
148142
{
@@ -152,7 +146,12 @@ class win32_thread
152146
this->id_ = 0;
153147
}
154148

155-
constexpr void detach()
149+
#if __cpp_constexpr >= 202207L
150+
// https://en.cppreference.com/w/cpp/compiler_support/23.html#cpp_constexpr_202207L
151+
// for reduce some warning purpose
152+
constexpr
153+
#endif
154+
void detach()
156155
{
157156
if (!this->joinable()) [[unlikely]]
158157
{
@@ -172,22 +171,26 @@ class win32_thread
172171
::std::ranges::swap(id_, other.id_);
173172
}
174173

175-
/**
176-
* @note Unlike std::thread::get_id, this method return the const reference.
177-
*/
178174
[[nodiscard]]
179-
constexpr auto &&get_id() const noexcept
175+
constexpr auto get_id() const noexcept
180176
{
181177
return this->id_;
182178
}
183179

184180
[[nodiscard]]
185-
constexpr auto &&native_handle() const noexcept
181+
constexpr auto native_handle() const noexcept
186182
{
187183
return this->handle_;
188184
}
189185

190-
static constexpr ::std::uint_least32_t hardware_concurrency() noexcept
186+
[[nodiscard]]
187+
static
188+
#if __cpp_constexpr >= 202207L
189+
// https://en.cppreference.com/w/cpp/compiler_support/23.html#cpp_constexpr_202207L
190+
// for reduce some warning purpose
191+
constexpr
192+
#endif
193+
::std::uint_least32_t hardware_concurrency() noexcept
191194
{
192195
::fast_io::win32::system_info si{};
193196
::fast_io::win32::GetSystemInfo(__builtin_addressof(si));
@@ -198,7 +201,13 @@ class win32_thread
198201
namespace this_thread
199202
{
200203

201-
constexpr auto get_id() noexcept -> ::fast_io::win32::win32_thread::id
204+
[[nodiscard]]
205+
#if __cpp_constexpr >= 202207L
206+
// https://en.cppreference.com/w/cpp/compiler_support/23.html#cpp_constexpr_202207L
207+
// for reduce some warning purpose
208+
constexpr
209+
#endif
210+
auto get_id() noexcept -> ::fast_io::win32::win32_thread::id
202211
{
203212
return ::fast_io::win32::GetCurrentThreadId();
204213
}

0 commit comments

Comments
 (0)