xtd 0.2.0
Loading...
Searching...
No Matches
thread.h
Go to the documentation of this file.
1
4#pragma once
6#include "wait_handle.h"
7#include "thread_priority.h"
8#include "thread_start.h"
9#include "thread_state.h"
10#include "../as.h"
11#include "../core_export.h"
12#include "../object.h"
13#include "../time_span.h"
14#include "../types.h"
15
17namespace xtd {
19 namespace threading {
21 class thread_pool;
23
41 class core_export_ thread final : public object {
42 struct data;
43
44 struct static_data;
45
46 using thread_collection = std::vector<std::shared_ptr<thread>>;
47
48 public:
50
54 static const intptr invalid_handle;
55
60
62
80 explicit thread(const xtd::threading::thread_start& start);
86 thread(const xtd::threading::thread_start& start, int32 max_stack_size);
88
90 thread();
91 template <typename start_t>
92 thread(start_t start) : thread(parameterized_thread_start {start}) {}
93 template <typename start_t>
94 thread(start_t start, int32 max_stack_size) : thread(parameterized_thread_start {start}, max_stack_size) {}
95 thread(thread&&) = default;
96 thread(const thread&) = default;
97 thread& operator=(const thread&);
98 ~thread();
100
102
104
107 intptr handle() const noexcept;
108
111 bool is_alive() const noexcept;
112
123 bool is_background() const noexcept;
134 thread& is_background(bool value);
135
138 bool is_main_thread() const noexcept;
139
143 bool is_thread_pool_thread() const noexcept;
144
149 bool joinable() const noexcept;
150
155 int32 managed_thread_id() const noexcept;
156
159 ustring name() const noexcept;
162 thread& name(const ustring& value);
163
174 xtd::threading::thread_priority priority() const noexcept;
185 thread& priority(xtd::threading::thread_priority value);
186
190 intptr thread_id() const noexcept;
191
196 xtd::threading::thread_state thread_state() const noexcept;
198
200
204 static thread& current_thread() noexcept;
205
209 static thread& main_thread();
211
213
217 void abort();
218
221 void detach();
222
226 void interrupt();
227
230 void join();
231
237 bool join(int32 milliseconds_timeout);
238
244 bool join(const time_span& timeout);
245
250 void resume();
251
254 void start();
255
259 void start(std::any obj);
260
265 void suspend();
267
269
274 static thread start_new(const xtd::threading::thread_start& start);
279 static thread start_new(const xtd::threading::parameterized_thread_start& start, std::any obj);
280
286 static void join_all();
293 static bool join_all(int32 milliseconds_timeout);
300 static bool join_all(const time_span& timeout);
301
305 template<typename collection_t>
306 static void join_all(const collection_t& threads) {join_all(threads, timeout::infinite);}
312 template<typename collection_t>
313 static bool join_all(const collection_t& threads, int32 milliseconds_timeout) {
314 std::vector<thread*> thread_pointers;
315 for (auto& item : threads)
316 thread_pointers.push_back(const_cast<thread*>(&item));
317 return join_all_ptr(thread_pointers, milliseconds_timeout);
318 }
324 template<typename collection_t>
325 static bool join_all(const collection_t& threads, const time_span& timeout) {return join_all(threads, as<int32>(timeout.total_milliseconds_duration().count()));}
326
330 static void sleep(int32 milliseconds_timeout);
331
335 static void sleep(const time_span& timeout);
336
342 static void spin_wait(int32 iterations);
343
349 static bool yield();
351
353 template<typename item_t>
354 static bool join_all(const std::initializer_list<item_t>& threads) {return join_all(threads, timeout::infinite);}
355 template<typename item_t>
356 static bool join_all(const std::initializer_list<item_t>& threads, int32 milliseconds_timeout) {
357 std::vector<thread*> thread_pointers;
358 for (auto& item : threads)
359 thread_pointers.push_back(const_cast<thread*>(&item));
360 return join_all_ptr(thread_pointers, milliseconds_timeout);
361 }
362 template<typename item_t>
363 static bool join_all(const std::initializer_list<item_t>& threads, const time_span& timeout) {return join_all(threads, as<int32>(timeout.total_milliseconds_duration().count()));}
364 static bool join_all(const std::initializer_list<std::shared_ptr<thread>>& threads);
365 static bool join_all(const std::initializer_list<std::shared_ptr<thread>>& threads, int32 milliseconds_timeout);
366 static bool join_all(const std::initializer_list<std::shared_ptr<thread>>& threads, const time_span& timeout);
367 static bool join_all(const std::initializer_list<std::unique_ptr<thread>>& threads);
368 static bool join_all(const std::initializer_list<std::unique_ptr<thread>>& threads, int32 milliseconds_timeout);
369 static bool join_all(const std::initializer_list<std::unique_ptr<thread>>& threads, const time_span& timeout);
370 static bool join_all(const std::vector<std::shared_ptr<thread>>& threads);
371 static bool join_all(const std::vector<std::shared_ptr<thread>>& threads, int32 milliseconds_timeout);
372 static bool join_all(const std::vector<std::shared_ptr<thread>>& threads, const time_span& timeout);
373 static bool join_all(const std::vector<std::unique_ptr<thread>>& threads);
374 static bool join_all(const std::vector<std::unique_ptr<thread>>& threads, int32 milliseconds_timeout);
375 static bool join_all(const std::vector<std::unique_ptr<thread>>& threads, const time_span& timeout);
376 template <typename start_t>
377 static thread start_new(start_t start) {return start_new(thread_start {start});}
378 template <typename start_t>
379 static thread start_new(start_t start, std::any obj) {return start_new(parameterized_thread_start {start}, obj);}
381
382 private:
383 friend class thread_pool;
384 friend class wait_handle;
385
386 void close();
387 static bool do_wait(wait_handle& wait_handle, int32 milliseconds_timeout);
388 static int32 generate_managed_thread_id() noexcept;
389 static intptr get_current_thread_handle();
390 static intptr get_current_thread_id() noexcept;
391 static static_data& get_static_data();
392 static thread& get_thread(intptr thread_id);
393 void interrupt_internal();
394 bool is_aborted() const noexcept;
395 bool is_stopped() const noexcept;
396 bool is_suspended() const noexcept;
397 void is_thread_pool_thread(bool value) noexcept;
398 bool is_unmanaged_thread() const noexcept;
399 bool is_unstarted() const noexcept;
400 bool is_wait_sleep_join() const noexcept;
401 static bool join_all_ptr(const std::vector<thread*>& threads, int32 milliseconds_timeout);
402 void thread_proc();
403 static thread& unmanaged_thread();
404
405 static constexpr int32 main_managed_thread_id = 1;
406 static constexpr int32 unmanaged_thread_id = 0;
407
408 std::shared_ptr<data> data_;
409 static intptr main_thread_id_;
410 };
411 }
412}
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.h:32
Creates and controls a thread, sets its priority, and gets its status.
Definition thread.h:41
thread(const xtd::threading::parameterized_thread_start &start)
Initializes a new instance of the xtd::threading::thread class, specifying a delegate that allows an ...
thread(const xtd::threading::thread_start &start)
Initializes a new instance of the xtd::threading::thread class.
static void join_all(const collection_t &threads)
Blocks the calling thread until all specified joinable threads collection terminate.
Definition thread.h:306
intptr handle() const noexcept
Gets the native operating system handle.
static void sleep(int32 milliseconds_timeout)
Suspends the current thread for a specified time.
static const intptr invalid_handle
Represents an invalid native operating system handle. This field is read-only.
Definition thread.h:54
static bool join_all(const collection_t &threads, const time_span &timeout)
Blocks the calling thread until all specified joinable threads collection terminate or the specified ...
Definition thread.h:325
static void spin_wait(int32 iterations)
Causes a thread to wait the number of times defined by the iterations parameter.
static bool yield()
Causes the calling thread to yield execution to another thread that is ready to run on the current pr...
static bool join_all(const collection_t &threads, int32 milliseconds_timeout)
Blocks the calling thread until all specified joinable threads collection terminate or the specified ...
Definition thread.h:313
static const intptr invalid_thread_id
Represents an invalid native operating system thread id. This field is read-only.
Definition thread.h:58
static void sleep(const time_span &timeout)
Suspends the current thread for a specified time.
thread(const xtd::threading::thread_start &start, int32 max_stack_size)
Initializes a new instance of the xtd::threading::thread class, specifying the maximum stack size for...
thread(const xtd::threading::parameterized_thread_start &start, int32 max_stack_size)
Initializes a new instance of the xtd::threading::thread class, specifying a delegate that allows an ...
Contains a constant used to specify an infinite amount of time. This class cannot be inherited.
Definition timeout.h:31
Represents text as a sequence of UTF-8 code units.
Definition ustring.h:47
#define core_export_
Define shared library export.
Definition core_export.h:13
thread_state
Specifies the execution states of a System::Threading::Thread.
Definition thread_state.h:22
thread_priority
Specifies the scheduling priority of a system::threading::thread.
Definition thread_priority.h:22
int_least32_t int32
Represents a 32-bit signed integer.
Definition types.h:131
intmax_t intptr
Represent a pointer or a handle.
Definition types.h:153
delegate< void(std::any)> parameterized_thread_start
Represents the method that executes on a xtd::threading::thread.
Definition parameterized_thread_start.h:24
delegate< void()> thread_start
Represents the method that executes on a xtd::threading::thread.
Definition thread_start.h:22
@ start
Starting of a logical operation.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Contains xtd::threading::parameterized_thread_start exception.
Represents a time interval.
Definition time_span.h:26
std::chrono::milliseconds total_milliseconds_duration() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional milliseconds...
Contains xtd::threading::thread_priority enumeration.
Contains xtd::threading::thread_start exception.
Contains xtd::threading::thread_state enumeration.
Contains xtd::threading::wait_handle exception.