Skip to content

Commit 525fdc1

Browse files
default constructor for nt/win32 thread
- add example for fast_io::thread
1 parent 3ce234a commit 525fdc1

3 files changed

Lines changed: 75 additions & 36 deletions

File tree

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/nt.h

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,11 @@ class nt_thread
6666
using native_handle_type = void *;
6767

6868
private:
69-
id id_;
70-
native_handle_type handle_;
69+
id id_{nullptr};
70+
native_handle_type handle_{nullptr};
7171

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

7775
template <typename Func, typename... Args>
7876
requires(::std::invocable<Func, Args...>)
@@ -115,12 +113,7 @@ class nt_thread
115113

116114
constexpr nt_thread(nt_thread const &) noexcept = delete;
117115

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-
}
116+
constexpr nt_thread(nt_thread &&other) noexcept = default;
124117

125118
constexpr ~nt_thread() noexcept
126119
{
@@ -189,17 +182,14 @@ class nt_thread
189182
::std::ranges::swap(id_, other.id_);
190183
}
191184

192-
/**
193-
* @note Unlike std::thread::get_id, this method return the const reference.
194-
*/
195185
[[nodiscard]]
196-
constexpr auto &&get_id() const noexcept
186+
constexpr auto get_id() const noexcept
197187
{
198188
return this->id_;
199189
}
200190

201191
[[nodiscard]]
202-
constexpr auto &&native_handle() const noexcept
192+
constexpr auto native_handle() const noexcept
203193
{
204194
return this->handle_;
205195
}

include/fast_io_hosted/threads/thread/win32.h

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,11 @@ class win32_thread
6565
using native_handle_type = void *;
6666

6767
private:
68-
id id_;
69-
native_handle_type handle_;
68+
id id_{};
69+
native_handle_type handle_{nullptr};
7070

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

7674
template <typename Func, typename... Args>
7775
requires(::std::invocable<Func, Args...>)
@@ -104,12 +102,7 @@ class win32_thread
104102

105103
constexpr win32_thread(win32_thread const &) noexcept = delete;
106104

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-
}
105+
constexpr win32_thread(win32_thread &&other) noexcept = default;
113106

114107
constexpr ~win32_thread() noexcept
115108
{
@@ -142,7 +135,12 @@ class win32_thread
142135
return this->id_ != 0;
143136
}
144137

145-
constexpr void join()
138+
#if __cpp_constexpr >= 202207L
139+
// https://en.cppreference.com/w/cpp/compiler_support/23.html#cpp_constexpr_202207L
140+
// for reduce some warning purpose
141+
constexpr
142+
#endif
143+
void join()
146144
{
147145
if (!this->joinable()) [[unlikely]]
148146
{
@@ -152,7 +150,12 @@ class win32_thread
152150
this->id_ = 0;
153151
}
154152

155-
constexpr void detach()
153+
#if __cpp_constexpr >= 202207L
154+
// https://en.cppreference.com/w/cpp/compiler_support/23.html#cpp_constexpr_202207L
155+
// for reduce some warning purpose
156+
constexpr
157+
#endif
158+
void detach()
156159
{
157160
if (!this->joinable()) [[unlikely]]
158161
{
@@ -172,22 +175,26 @@ class win32_thread
172175
::std::ranges::swap(id_, other.id_);
173176
}
174177

175-
/**
176-
* @note Unlike std::thread::get_id, this method return the const reference.
177-
*/
178178
[[nodiscard]]
179-
constexpr auto &&get_id() const noexcept
179+
constexpr auto get_id() const noexcept
180180
{
181181
return this->id_;
182182
}
183183

184184
[[nodiscard]]
185-
constexpr auto &&native_handle() const noexcept
185+
constexpr auto native_handle() const noexcept
186186
{
187187
return this->handle_;
188188
}
189189

190-
static constexpr ::std::uint_least32_t hardware_concurrency() noexcept
190+
[[nodiscard]]
191+
static
192+
#if __cpp_constexpr >= 202207L
193+
// https://en.cppreference.com/w/cpp/compiler_support/23.html#cpp_constexpr_202207L
194+
// for reduce some warning purpose
195+
constexpr
196+
#endif
197+
::std::uint_least32_t hardware_concurrency() noexcept
191198
{
192199
::fast_io::win32::system_info si{};
193200
::fast_io::win32::GetSystemInfo(__builtin_addressof(si));
@@ -198,7 +205,13 @@ class win32_thread
198205
namespace this_thread
199206
{
200207

201-
constexpr auto get_id() noexcept -> ::fast_io::win32::win32_thread::id
208+
[[nodiscard]]
209+
#if __cpp_constexpr >= 202207L
210+
// https://en.cppreference.com/w/cpp/compiler_support/23.html#cpp_constexpr_202207L
211+
// for reduce some warning purpose
212+
constexpr
213+
#endif
214+
auto get_id() noexcept -> ::fast_io::win32::win32_thread::id
202215
{
203216
return ::fast_io::win32::GetCurrentThreadId();
204217
}

0 commit comments

Comments
 (0)