xtd 0.2.0
thread.hpp
Go to the documentation of this file.
1
4#pragma once
6#include "wait_handle.hpp"
7#include "thread_priority.hpp"
8#include "thread_start.hpp"
9#include "thread_state.hpp"
10#include "../as.hpp"
11#include "../core_export.hpp"
12#include "../date_time.hpp"
13#include "../intptr.hpp"
14#include "../object.hpp"
15#include "../time_span.hpp"
16#include "../types.hpp"
17
19namespace xtd {
21 namespace threading {
23 class thread_pool;
25
45 class core_export_ thread : public object {
46 struct data;
47
48 struct static_data;
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<class start_t>
94 explicit thread(start_t start) : thread(parameterized_thread_start {start}) {}
95 template<class 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
236 xtd::size get_hash_code() const noexcept override;
237
241 void interrupt();
242
245 void join();
246
252 bool join(int32 milliseconds_timeout);
253
259 bool join(const time_span& timeout);
260
265 void resume();
266
269 void start();
270
274 void start(const xtd::any_object& obj);
275
280 void suspend();
282
284
289 static thread start_new(const xtd::threading::thread_start& start);
294 static thread start_new(const xtd::threading::parameterized_thread_start& start, const xtd::any_object& obj);
295
301 static void join_all();
308 static bool join_all(int32 milliseconds_timeout);
315 static bool join_all(const time_span& timeout);
316
320 template<class collection_t>
321 static void join_all(const collection_t& threads) {join_all(threads, timeout::infinite);}
327 template<class collection_t>
328 static bool join_all(const collection_t& threads, int32 milliseconds_timeout) {
329 auto thread_pointers = xtd::array<thread*> {};
330 for (auto& item : threads)
331 thread_pointers.resize(thread_pointers.size() + 1, const_cast<thread*>(&item));
332 return join_all_ptr(thread_pointers, milliseconds_timeout);
333 }
339 template<class collection_t>
340 static bool join_all(const collection_t& threads, const time_span& timeout) {return join_all(threads, as<int32>(timeout.total_milliseconds_duration().count()));}
341
345 static void sleep(int32 milliseconds_timeout);
346
350 static void sleep(const time_span& timeout);
351
357 static void spin_wait(int32 iterations);
358
364 static bool yield() noexcept;
366
368 template<class item_t>
369 static bool join_all(const std::initializer_list<item_t>& threads) {return join_all(threads, timeout::infinite);}
370 template<class item_t>
371 static bool join_all(const std::initializer_list<item_t>& threads, int32 milliseconds_timeout) {
372 auto thread_pointers = xtd::array<thread*> {};
373 for (auto& item : threads)
374 thread_pointers.resize(thread_pointers.size() + 1, const_cast<thread*>(&item));
375 return join_all_ptr(thread_pointers, milliseconds_timeout);
376 }
377 template<class item_t>
378 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()));}
379 static bool join_all(const std::initializer_list<xtd::sptr<thread>>& threads);
380 static bool join_all(const std::initializer_list<xtd::sptr<thread>>& threads, int32 milliseconds_timeout);
381 static bool join_all(const std::initializer_list<xtd::sptr<thread>>& threads, const time_span& timeout);
382 static bool join_all(const std::initializer_list<xtd::uptr<thread>>& threads);
383 static bool join_all(const std::initializer_list<xtd::uptr<thread>>& threads, int32 milliseconds_timeout);
384 static bool join_all(const std::initializer_list<xtd::uptr<thread>>& threads, const time_span& timeout);
385 static bool join_all(const xtd::array<xtd::sptr<thread>>& threads);
386 static bool join_all(const xtd::array<xtd::sptr<thread>>& threads, int32 milliseconds_timeout);
387 static bool join_all(const xtd::array<xtd::sptr<thread>>& threads, const time_span& timeout);
388 static bool join_all(const std::vector<xtd::uptr<thread>>& threads);
389 static bool join_all(const std::vector<xtd::uptr<thread>>& threads, int32 milliseconds_timeout);
390 static bool join_all(const std::vector<xtd::uptr<thread>>& threads, const time_span& timeout);
391 template<class start_t>
392 static thread start_new(start_t start) {return start_new(thread_start {start});}
393 template<class start_t>
394 static thread start_new(start_t start, const xtd::any_object& obj) {return start_new(parameterized_thread_start {start}, obj);}
396
397 private:
398 friend class thread_pool;
399 friend class wait_handle;
400
401 void close();
402 static bool do_wait(wait_handle& wait_handle, int32 milliseconds_timeout);
403 static int32 generate_managed_thread_id() noexcept;
404 static intptr get_current_thread_handle();
405 static intptr get_current_thread_id() noexcept;
406 static static_data& get_static_data();
407 static thread& get_thread(intptr thread_id);
408 void interrupt_internal();
409 bool is_aborted() const noexcept;
410 bool is_stopped() const noexcept;
411 bool is_suspended() const noexcept;
412 void is_thread_pool_thread(bool value) noexcept;
413 bool is_unmanaged_thread() const noexcept;
414 bool is_unstarted() const noexcept;
415 bool is_wait_sleep_join() const noexcept;
416 static bool join_all_ptr(const xtd::array<thread*>& threads, int32 milliseconds_timeout);
417 void thread_proc();
418 static thread& unmanaged_thread();
419
420 static constexpr int32 main_managed_thread_id = 1;
421 static constexpr int32 unmanaged_thread_id = 0;
422
423 xtd::sptr<data> data_;
424 static intptr main_thread_id_;
425 };
426
428 namespace this_thread {
430
443 xtd::intptr handle() noexcept;
444
456 xtd::int32 managed_thread_id() noexcept;
457
469 xtd::string name() noexcept;
481 void name(const xtd::string& name);
482
494 xtd::threading::thread_priority priority() noexcept;
506 void priority(xtd::threading::thread_priority priority);
507
519 xtd::intptr thread_id() noexcept;
521
523
536 xtd::intptr get_id() noexcept;
537
550 void sleep_for(const xtd::time_span& sleep_duration);
551
564 void sleep_until(const xtd::date_time& sleep_time);
565
577 bool yield() noexcept;
579 }
580 }
581}
Represent a polymorphic wrapper capable of holding any type.
Definition any_object.hpp:29
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:61
void resize(size_type new_size)
Resizes the container to contain count elements, does nothing if count == size(). @param new_size The...
Definition basic_array.hpp:377
Represents an instant in time, typically expressed as a date and time of day.
Definition date_time.hpp:85
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:43
The xtd::shared_ptr_object is a shared pointer as std::shared_ptr.
Definition shared_ptr_object.hpp:30
Creates and controls a thread, sets its priority, and gets its status.
Definition thread.hpp:45
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.hpp:321
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.hpp: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.hpp:340
static void spin_wait(int32 iterations)
Causes a thread to wait the number of times defined by the iterations parameter.
static bool yield() noexcept
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.hpp:328
static const intptr invalid_thread_id
Represents an invalid native operating system thread id. This field is read-only.
Definition thread.hpp: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.hpp:33
Represents a time interval.
Definition time_span.hpp:29
xtd::delegate< void(const xtd::any_object &obj)> parameterized_thread_start
Represents the method that executes on a xtd::threading::thread.
Definition parameterized_thread_start.hpp:29
xtd::delegate< void()> thread_start
Represents the method that executes on a xtd::threading::thread.
Definition thread_start.hpp:24
#define core_export_
Define shared library export.
Definition core_export.hpp:13
xtd::intptr handle() noexcept
Gets the thread handle of the current thread.
thread_state
Specifies the execution states of a System::Threading::Thread.
Definition thread_state.hpp:24
thread_priority
Specifies the scheduling priority of a system::threading::thread.
Definition thread_priority.hpp:24
int32_t int32
Represents a 32-bit signed integer.
Definition int32.hpp:23
std::unique_ptr< type_t > uptr
The xtd::uptr object is a unique pointer.
Definition uptr.hpp:25
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
intmax_t intptr
Represent a pointer or a handle.
Definition intptr.hpp:23
@ start
Starting of a logical operation.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10
Contains xtd::threading::parameterized_thread_start delegate.
Contains xtd::threading::thread_priority enumeration.
Contains xtd::threading::thread_start delegate.
Contains xtd::threading::thread_state enumeration.
Contains xtd::threading::wait_handle exception.