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
43 class core_export_ thread : public object {
44 struct data;
45
46 struct static_data;
47
48 using thread_collection = std::vector<xtd::sptr<thread>>;
49
50 public:
52
56 static const intptr invalid_handle;
57
62
64
82 explicit thread(const xtd::threading::thread_start& start);
88 thread(const xtd::threading::thread_start& start, int32 max_stack_size);
90
92 thread();
93 template <typename start_t>
94 explicit thread(start_t start) : thread(parameterized_thread_start {start}) {}
95 template <typename start_t>
96 thread(start_t start, int32 max_stack_size) : thread(parameterized_thread_start {start}, max_stack_size) {}
97 thread(thread&&) = default;
98 thread(const thread&) = default;
99 thread& operator=(const thread&);
100 ~thread();
102
104
108 bool auto_join() const noexcept;
112 thread& auto_join(bool value);
113
116 intptr handle() const noexcept;
117
120 bool is_alive() const noexcept;
121
132 bool is_background() const noexcept;
144 thread& is_background(bool value);
145
148 bool is_main_thread() const noexcept;
149
153 bool is_thread_pool_thread() const noexcept;
154
159 bool joinable() const noexcept;
160
165 int32 managed_thread_id() const noexcept;
166
169 string name() const noexcept;
173 thread& name(const string& value);
174
185 xtd::threading::thread_priority priority() const noexcept;
196 thread& priority(xtd::threading::thread_priority value);
197
201 intptr thread_id() const noexcept;
202
207 xtd::threading::thread_state thread_state() const noexcept;
209
211
215 static thread& current_thread() noexcept;
216
220 static thread& main_thread();
222
224
228 void abort();
229
232 void detach();
233
237 void interrupt();
238
241 void join();
242
248 bool join(int32 milliseconds_timeout);
249
255 bool join(const time_span& timeout);
256
261 void resume();
262
265 void start();
266
270 void start(std::any obj);
271
276 void suspend();
278
280
285 static thread start_new(const xtd::threading::thread_start& start);
290 static thread start_new(const xtd::threading::parameterized_thread_start& start, std::any obj);
291
297 static void join_all();
304 static bool join_all(int32 milliseconds_timeout);
311 static bool join_all(const time_span& timeout);
312
316 template<typename collection_t>
317 static void join_all(const collection_t& threads) {join_all(threads, timeout::infinite);}
323 template<typename collection_t>
324 static bool join_all(const collection_t& threads, int32 milliseconds_timeout) {
325 std::vector<thread*> thread_pointers;
326 for (auto& item : threads)
327 thread_pointers.push_back(const_cast<thread*>(&item));
328 return join_all_ptr(thread_pointers, milliseconds_timeout);
329 }
335 template<typename collection_t>
336 static bool join_all(const collection_t& threads, const time_span& timeout) {return join_all(threads, as<int32>(timeout.total_milliseconds_duration().count()));}
337
341 static void sleep(int32 milliseconds_timeout);
342
346 static void sleep(const time_span& timeout);
347
353 static void spin_wait(int32 iterations);
354
360 static bool yield();
362
364 template<typename item_t>
365 static bool join_all(const std::initializer_list<item_t>& threads) {return join_all(threads, timeout::infinite);}
366 template<typename item_t>
367 static bool join_all(const std::initializer_list<item_t>& threads, int32 milliseconds_timeout) {
368 std::vector<thread*> thread_pointers;
369 for (auto& item : threads)
370 thread_pointers.push_back(const_cast<thread*>(&item));
371 return join_all_ptr(thread_pointers, milliseconds_timeout);
372 }
373 template<typename item_t>
374 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()));}
375 static bool join_all(const std::initializer_list<xtd::sptr<thread>>& threads);
376 static bool join_all(const std::initializer_list<xtd::sptr<thread>>& threads, int32 milliseconds_timeout);
377 static bool join_all(const std::initializer_list<xtd::sptr<thread>>& threads, const time_span& timeout);
378 static bool join_all(const std::initializer_list<xtd::uptr<thread>>& threads);
379 static bool join_all(const std::initializer_list<xtd::uptr<thread>>& threads, int32 milliseconds_timeout);
380 static bool join_all(const std::initializer_list<xtd::uptr<thread>>& threads, const time_span& timeout);
381 static bool join_all(const std::vector<xtd::sptr<thread>>& threads);
382 static bool join_all(const std::vector<xtd::sptr<thread>>& threads, int32 milliseconds_timeout);
383 static bool join_all(const std::vector<xtd::sptr<thread>>& threads, const time_span& timeout);
384 static bool join_all(const std::vector<xtd::uptr<thread>>& threads);
385 static bool join_all(const std::vector<xtd::uptr<thread>>& threads, int32 milliseconds_timeout);
386 static bool join_all(const std::vector<xtd::uptr<thread>>& threads, const time_span& timeout);
387 template <typename start_t>
388 static thread start_new(start_t start) {return start_new(thread_start {start});}
389 template <typename start_t>
390 static thread start_new(start_t start, std::any obj) {return start_new(parameterized_thread_start {start}, obj);}
392
393 private:
394 friend class thread_pool;
395 friend class wait_handle;
396
397 void close();
398 static bool do_wait(wait_handle& wait_handle, int32 milliseconds_timeout);
399 static int32 generate_managed_thread_id() noexcept;
400 static intptr get_current_thread_handle();
401 static intptr get_current_thread_id() noexcept;
402 static static_data& get_static_data();
403 static thread& get_thread(intptr thread_id);
404 void interrupt_internal();
405 bool is_aborted() const noexcept;
406 bool is_stopped() const noexcept;
407 bool is_suspended() const noexcept;
408 void is_thread_pool_thread(bool value) noexcept;
409 bool is_unmanaged_thread() const noexcept;
410 bool is_unstarted() const noexcept;
411 bool is_wait_sleep_join() const noexcept;
412 static bool join_all_ptr(const std::vector<thread*>& threads, int32 milliseconds_timeout);
413 void thread_proc();
414 static thread& unmanaged_thread();
415
416 static constexpr int32 main_managed_thread_id = 1;
417 static constexpr int32 unmanaged_thread_id = 0;
418
419 xtd::sptr<data> data_;
420 static intptr main_thread_id_;
421 };
422 }
423}
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.h:42
Creates and controls a thread, sets its priority, and gets its status.
Definition thread.h:43
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:317
static void sleep(int32 milliseconds_timeout)
Suspends the current thread for a specified time.
bool auto_join() const noexcept
Gets a value indicating the current thread wiil be joined when destroyed.
static const intptr invalid_handle
Represents an invalid native operating system handle. This field is read-only.
Definition thread.h:56
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:336
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:324
static const intptr invalid_thread_id
Represents an invalid native operating system thread id. This field is read-only.
Definition thread.h:60
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:33
#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:24
thread_priority
Specifies the scheduling priority of a system::threading::thread.
Definition thread_priority.h:24
int32_t int32
Represents a 32-bit signed integer.
Definition int32.h:23
std::unique_ptr< type_t > uptr
The xtd::uptr object is a unique pointer.
Definition uptr.h:25
intmax_t intptr
Represent a pointer or a handle.
Definition intptr.h:23
std::shared_ptr< type_t > sptr
The xtd::sptr object is a shared pointer.
Definition sptr.h:25
delegate< void(std::any)> parameterized_thread_start
Represents the method that executes on a xtd::threading::thread.
Definition parameterized_thread_start.h:26
delegate< void()> thread_start
Represents the method that executes on a xtd::threading::thread.
Definition thread_start.h:24
@ 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:29
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.