xtd 0.2.0
thread.h
Go to the documentation of this file.
1 #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 
17 namespace 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 
58  static const intptr invalid_thread_id;
60 
62 
74  thread(const xtd::threading::parameterized_thread_start& start, int32 max_stack_size);
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 
106  static thread& current_thread();
107 
110  intptr handle() const noexcept;
111 
114  bool is_alive() const noexcept;
115 
126  bool is_background() const noexcept;
137  thread& is_background(bool value);
138 
141  bool is_main_thread() const noexcept;
142 
146  bool is_thread_pool_thread() const noexcept;
147 
152  bool joinable() const noexcept;
153 
157  static thread& main_thread();
158 
163  int32 managed_thread_id() const noexcept;
164 
167  ustring name() const noexcept;
170  thread& name(const ustring& value);
171 
182  xtd::threading::thread_priority priority() const noexcept;
193  thread& priority(xtd::threading::thread_priority value);
194 
198  intptr thread_id() const noexcept;
199 
204  xtd::threading::thread_state thread_state() const noexcept;
206 
208 
212  void abort();
213 
216  void detach();
217 
221  void interrupt();
222 
225  void join();
226 
232  bool join(int32 milliseconds_timeout);
233 
239  bool join(const time_span& timeout);
240 
246  static void join_all();
253  static bool join_all(int32 milliseconds_timeout);
260  static bool join_all(const time_span& timeout);
261 
265  template<typename collection_t>
266  static void join_all(const collection_t& threads) {join_all(threads, timeout::infinite);}
272  template<typename collection_t>
273  static bool join_all(const collection_t& threads, int32 milliseconds_timeout) {
274  std::vector<thread*> thread_pointers;
275  for (auto& item : threads)
276  thread_pointers.push_back(const_cast<thread*>(&item));
277  return join_all_ptr(thread_pointers, milliseconds_timeout);
278  }
284  template<typename collection_t>
285  static bool join_all(const collection_t& threads, const time_span& timeout) {return join_all(threads, as<int32>(timeout.total_milliseconds_duration().count()));}
286 
291  void resume();
292 
296  static void sleep(int32 milliseconds_timeout);
297 
301  static void sleep(const time_span& timeout);
302 
308  static void spin_wait(int32 iterations);
309 
312  void start();
313 
317  void start(std::any obj);
318 
322  static thread start_new(const xtd::threading::thread_start& start);
327  static thread start_new(const xtd::threading::parameterized_thread_start& start, std::any obj);
328 
333  void suspend();
334 
340  static bool yield();
342 
345  template<typename item_t>
346  static bool join_all(const std::initializer_list<item_t>& threads) {return join_all(threads, timeout::infinite);}
347  template<typename item_t>
348  static bool join_all(const std::initializer_list<item_t>& threads, int32 milliseconds_timeout) {
349  std::vector<thread*> thread_pointers;
350  for (auto& item : threads)
351  thread_pointers.push_back(const_cast<thread*>(&item));
352  return join_all_ptr(thread_pointers, milliseconds_timeout);
353  }
354  template<typename item_t>
355  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()));}
356  static bool join_all(const std::initializer_list<std::shared_ptr<thread>>& threads);
357  static bool join_all(const std::initializer_list<std::shared_ptr<thread>>& threads, int32 milliseconds_timeout);
358  static bool join_all(const std::initializer_list<std::shared_ptr<thread>>& threads, const time_span& timeout);
359  static bool join_all(const std::initializer_list<std::unique_ptr<thread>>& threads);
360  static bool join_all(const std::initializer_list<std::unique_ptr<thread>>& threads, int32 milliseconds_timeout);
361  static bool join_all(const std::initializer_list<std::unique_ptr<thread>>& threads, const time_span& timeout);
362  static bool join_all(const std::vector<std::shared_ptr<thread>>& threads);
363  static bool join_all(const std::vector<std::shared_ptr<thread>>& threads, int32 milliseconds_timeout);
364  static bool join_all(const std::vector<std::shared_ptr<thread>>& threads, const time_span& timeout);
365  static bool join_all(const std::vector<std::unique_ptr<thread>>& threads);
366  static bool join_all(const std::vector<std::unique_ptr<thread>>& threads, int32 milliseconds_timeout);
367  static bool join_all(const std::vector<std::unique_ptr<thread>>& threads, const time_span& timeout);
368  template <typename start_t>
369  static thread start_new(start_t start) {return start_new(thread_start {start});}
370  template <typename start_t>
371  static thread start_new(start_t start, std::any obj) {return start_new(parameterized_thread_start {start}, obj);}
373 
374  private:
375  friend class thread_pool;
376  friend class wait_handle;
377 
378  void close();
379  static bool do_wait(wait_handle& wait_handle, int32 milliseconds_timeout);
380  static int32 generate_managed_thread_id() noexcept;
381  static intptr get_current_thread_handle();
382  static intptr get_current_thread_id();
383  static static_data& get_static_data();
384  static thread& get_thread(intptr thread_id);
385  void interrupt_internal();
386  bool is_aborted() const noexcept;
387  bool is_stopped() const noexcept;
388  bool is_suspended() const noexcept;
389  void is_thread_pool_thread(bool value) noexcept;
390  bool is_unmanaged_thread() const noexcept;
391  bool is_unstarted() const noexcept;
392  bool is_wait_sleep_join() const noexcept;
393  static bool join_all_ptr(const std::vector<thread*>& threads, int32 milliseconds_timeout);
394  void thread_proc();
395  static thread& unmanaged_thread();
396 
397  static constexpr int32 main_managed_thread_id = 1;
398  static constexpr int32 unmanaged_thread_id = 0;
399 
400  std::shared_ptr<data> data_;
401  static intptr main_thread_id_;
402  };
403  }
404 }
static const intptr invalid_handle
Represents an invalid native operating system handle. This field is read-only.
Definition: thread.h:54
The Computer Sleep key.
intmax_t intptr
Represent a pointer or a handle.
Definition: types.h:151
Contains xtd::threading::wait_handle exception.
int32 as< int32 >(std::any value)
Casts a type into another type.
Definition: as.h:9228
Contains xtd::threading::parameterized_thread_start exception.
thread_state
Specifies the execution states of a System::Threading::Thread.
Definition: thread_state.h:22
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:273
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
Contains xtd::threading::thread_start exception.
std::chrono::milliseconds total_milliseconds_duration() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional milliseconds...
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:46
Provides a pool of threads that can be used to execute tasks, post work items, process asynchronous I...
Definition: thread_pool.h:50
thread_priority
Specifies the scheduling priority of a system::threading::thread.
Definition: thread_priority.h:22
static void join_all(const collection_t &threads)
Blocks the calling thread until all specified joinable threads collection terminate.
Definition: thread.h:266
static constexpr int32 infinite
A constant used to specify an infinite waiting period. This field is constant.
Definition: timeout.h:39
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:285
delegate< void(std::any)> parameterized_thread_start
Represents the method that executes on a xtd::threading::thread.
Definition: parameterized_thread_start.h:24
Indicates that all styles except allow_binary_specifier, allow_octal_specifier and allow_hex_specifie...
Creates and controls a thread, sets its priority, and gets its status.
Definition: thread.h:41
static const intptr invalid_thread_id
Represents an invalid native operating system thread id. This field is read-only. ...
Definition: thread.h:58
Contains xtd::threading::thread_state enumeration.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes...
Definition: object.h:32
Represents a time interval.
Definition: time_span.h:26
int_least32_t int32
Represents a 32-bit signed integer.
Definition: types.h:129
Contains a constant used to specify an infinite amount of time. This class cannot be inherited...
Definition: timeout.h:31
Contains xtd::threading::thread_priority enumeration.
delegate< void()> thread_start
Represents the method that executes on a xtd::threading::thread.
Definition: thread_start.h:22
Encapsulates operating system specific objects that wait for exclusive access to shared resources...
Definition: wait_handle.h:48