xtd 1.0.0
Loading...
Searching...
No Matches
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 class date_time;
23
25 namespace threading {
27 namespace tasks {
28 template<typename result_t = void>
29 class basic_task;
30 }
31 class thread_pool;
33
53 class core_export_ thread : public object {
54 struct data;
55
56 struct static_data;
57
58 public:
60
64 static const intptr invalid_handle;
65
70
72
98
100 thread();
101 template<typename start_t>
102 explicit thread(start_t start) : thread(parameterized_thread_start {start}) {}
103 template<typename start_t>
104 thread(start_t start, int32 max_stack_size) : thread(parameterized_thread_start {start}, max_stack_size) {}
105 thread(thread&&) = default;
106 thread(const thread&) = default;
107 auto operator=(thread&&) -> thread& = default;
108 auto operator=(const thread&) -> thread&;
109 ~thread();
111
113
117 [[nodiscard]] auto auto_join() const noexcept -> bool;
121 auto auto_join(bool value) -> thread&;
122
125 [[nodiscard]] auto handle() const noexcept -> intptr;
126
129 [[nodiscard]] auto is_alive() const noexcept -> bool;
130
141 [[nodiscard]] auto is_background() const noexcept -> bool;
153 auto is_background(bool value) -> thread&;
154
157 [[nodiscard]] auto is_main_thread() const noexcept -> bool;
158
162 [[nodiscard]] auto is_thread_pool_thread() const noexcept -> bool;
163
168 [[nodiscard]] auto joinable() const noexcept -> bool;
169
174 [[nodiscard]] auto managed_thread_id() const noexcept -> int32;
175
178 [[nodiscard]] auto name() const noexcept -> string;
182 auto name(const string& value) -> thread&;
183
194 [[nodiscard]] auto priority() const noexcept -> xtd::threading::thread_priority;
206
212 [[nodiscard]] auto processor_affinity() const noexcept -> const xtd::array<xtd::usize>&;
218 auto processor_affinity(const xtd::array<xtd::usize>& value) -> thread&;
219
223 [[nodiscard]] auto thread_id() const noexcept -> intptr;
224
229 [[nodiscard]] auto thread_state() const noexcept -> xtd::threading::thread_state;
231
233
237 [[nodiscard]] static auto current_thread() noexcept -> thread&;
239
241
245 auto abort() -> void;
246
249 auto detach() -> void;
250
253 [[nodiscard]] auto get_hash_code() const noexcept -> xtd::usize override;
254
258 auto interrupt() -> void;
259
262 auto join() -> void;
263
269 auto join(int32 milliseconds_timeout) -> bool;
270
276 auto join(const time_span& timeout) -> bool;
277
282 auto resume() -> void;
283
286 auto start() -> void;
287
291 auto start(const xtd::any_object& obj) -> void;
292
297 auto suspend() -> void;
299
301
306 static auto start_new(const xtd::threading::thread_start& start) -> thread;
312
318 static auto join_all() -> void;
325 static auto join_all(int32 milliseconds_timeout) -> bool;
332 static auto join_all(const time_span& timeout) -> bool;
333
337 template<typename collection_t>
338 static auto join_all(const collection_t& threads) -> void {join_all(threads, timeout::infinite);}
344 template<typename collection_t>
345 static auto join_all(const collection_t& threads, int32 milliseconds_timeout) -> bool {
346 auto thread_pointers = xtd::array<thread*> {};
347 for (auto& item : threads) {
348 if (item.thread_id() == get_current_thread_id()) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::argument);
349 thread_pointers.resize(thread_pointers.length() + 1, const_cast<thread*>(&item));
350 }
351 return join_all_ptr(thread_pointers, milliseconds_timeout);
352 }
353
358 template<typename collection_t>
359 static auto join_all(const collection_t& threads, const time_span& timeout) -> bool {return join_all(threads, as<int32>(timeout.total_milliseconds_duration().count()));}
360
364 static auto sleep(int32 milliseconds_timeout) -> void;
365
369 static auto sleep(const time_span& timeout) -> void;
370
376 static auto spin_wait(int32 iterations) -> void;
377
383 static auto yield() noexcept -> bool;
385
387 template<typename item_t>
388 static auto join_all(const std::initializer_list<item_t>& threads) -> bool {return join_all(threads, timeout::infinite);}
389 template<typename item_t>
390 static auto join_all(const std::initializer_list<item_t>& threads, int32 milliseconds_timeout) -> bool {
391 auto thread_pointers = xtd::array<thread*> {};
392 for (auto& item : threads) {
393 if (item.thread_id() == get_current_thread_id()) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::argument);
394 thread_pointers.resize(thread_pointers.length() + 1, const_cast<thread*>(&item));
395 }
396 return join_all_ptr(thread_pointers, milliseconds_timeout);
397 }
398 template<typename item_t>
399 static auto join_all(const std::initializer_list<item_t>& threads, const time_span& timeout) -> bool {return join_all(threads, as<int32>(timeout.total_milliseconds_duration().count()));}
400 static auto join_all(const std::initializer_list<xtd::sptr<thread>>& threads) -> bool;
401 static auto join_all(const std::initializer_list<xtd::sptr<thread>>& threads, int32 milliseconds_timeout) -> bool;
402 static auto join_all(const std::initializer_list<xtd::sptr<thread>>& threads, const time_span& timeout) -> bool;
403 static auto join_all(const std::initializer_list<xtd::uptr<thread>>& threads) -> bool;
404 static auto join_all(const std::initializer_list<xtd::uptr<thread>>& threads, int32 milliseconds_timeout) -> bool;
405 static auto join_all(const std::initializer_list<xtd::uptr<thread>>& threads, const time_span& timeout) -> bool;
406 static auto join_all(const xtd::array<xtd::sptr<thread>>& threads) -> bool;
407 static auto join_all(const xtd::array<xtd::sptr<thread>>& threads, int32 milliseconds_timeout) -> bool;
408 static auto join_all(const xtd::array<xtd::sptr<thread>>& threads, const time_span& timeout) -> bool;
409 static auto join_all(const xtd::array<xtd::uptr<thread>>& threads) -> bool;
410 static auto join_all(const xtd::array<xtd::uptr<thread>>& threads, int32 milliseconds_timeout) -> bool;
411 static auto join_all(const xtd::array<xtd::uptr<thread>>& threads, const time_span& timeout) -> bool;
412 template<typename start_t>
413 static auto start_new(start_t start) -> thread {return start_new(thread_start {start});}
414 template<typename start_t>
415 static auto start_new(start_t start, const xtd::any_object& obj) -> thread {return start_new(parameterized_thread_start {start}, obj);}
417
418 private:
419 friend class thread_pool;
420 friend class wait_handle;
421 template<typename result_t>
422 friend class tasks::basic_task;
423
424 void close();
425 static auto do_wait(wait_handle& wait_handle, int32 milliseconds_timeout) -> bool;
426 [[nodiscard]] static auto generate_managed_thread_id() noexcept -> int32;
427 [[nodiscard]] static auto get_current_thread_handle() -> intptr;
428 [[nodiscard]] static auto get_current_thread_id() noexcept -> intptr;
429 [[nodiscard]] static auto get_static_data() -> static_data&;
430 [[nodiscard]] static auto get_thread(intptr thread_id) -> thread&;
431 [[nodiscard]] auto get_managed_thread_id() noexcept -> int32&;
432 auto interrupt_internal() -> void;
433 [[nodiscard]] auto is_aborted() const noexcept -> bool;
434 [[nodiscard]] auto is_stopped() const noexcept -> bool;
435 [[nodiscard]] auto is_suspended() const noexcept -> bool;
436 auto is_thread_pool_thread(bool value) noexcept -> void;
437 [[nodiscard]] auto is_unmanaged_thread() const noexcept -> bool;
438 [[nodiscard]] auto is_unstarted() const noexcept -> bool;
439 [[nodiscard]] auto is_wait_sleep_join() const noexcept -> bool;
440 static auto join_all_ptr(const xtd::array<thread*>& threads, int32 milliseconds_timeout) -> bool;
441 [[nodiscard]] static auto main_thread() -> thread&;
442 static auto set_main_managed_thread_id(int32 id) -> void {main_managed_thread_id = id;}
443 auto thread_proc() -> void;
444 [[nodiscard]] static auto unmanaged_thread() -> thread&;
445
446 inline static int32 main_managed_thread_id = 1;
447 static constexpr int32 unmanaged_thread_id = 0;
448
449 xtd::sptr<data> data_;
450 static intptr main_thread_id_;
451 };
452 }
453}
454
455#include "this_thread.hpp"
Contains xtd::as method.
Represent a polymorphic wrapper capable of holding any type.
Definition any_object.hpp:30
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:64
Represents an instant in time, typically expressed as a date and time of day.
Definition date_time.hpp:83
static auto throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current()) -> void
Throws an exption with specified exception case.
object()=default
Create a new instance of the ultimate base class object.
Provides a pool of threads that can be used to execute tasks, post work items, process asynchronous I...
Definition thread_pool.hpp:52
Creates and controls a thread, sets its priority, and gets its status.
Definition thread.hpp:53
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 auto join_all(const collection_t &threads) -> void
Blocks the calling thread until all specified joinable threads collection terminate.
Definition thread.hpp:338
auto joinable() const noexcept -> bool
Gets a value indicating the current thread is joinable.
auto is_background() const noexcept -> bool
Gets a value indicating whether or not a thread is a background thread.
static auto yield() noexcept -> bool
Causes the calling thread to yield execution to another thread that is ready to run on the current pr...
auto handle() const noexcept -> intptr
Gets the native operating system handle.
auto detach() -> void
Sets the thread background.
auto is_main_thread() const noexcept -> bool
Gets a value indicating the current thread is the main thread.
auto is_thread_pool_thread() const noexcept -> bool
Gets a value indicating whether or not a thread belongs to the managed thread pool.
static auto sleep(int32 milliseconds_timeout) -> void
Suspends the current thread for a specified time.
static auto spin_wait(int32 iterations) -> void
Causes a thread to wait the number of times defined by the iterations parameter.
auto suspend() -> void
Either suspends the thread, or if the thread is already suspended, has no effect (Should not be used)...
auto get_hash_code() const noexcept -> xtd::usize override
Serves as a hash function for a particular type.
auto abort() -> void
Raises a xtd::threading::thread_aborted_exception in the thread on which it is invoked,...
static auto current_thread() noexcept -> thread &
Gets the currently running thread.
static const intptr invalid_handle
Represents an invalid native operating system handle. This field is read-only.
Definition thread.hpp:64
static auto start_new(const xtd::threading::thread_start &start) -> thread
Create and immedialtely start a xtd::threading::thread with specified method.
auto join() -> void
Blocks the calling thread until this thread object terminates, while continuing to perform standard C...
auto managed_thread_id() const noexcept -> int32
Gets a unique identifier for the current managed thread.
auto start() -> void
Causes the operating system to change the state of the current instance to xtd::threading::thread_sta...
static auto join_all(const collection_t &threads, int32 milliseconds_timeout) -> bool
Blocks the calling thread until all specified joinable threads collection terminate or the specified ...
Definition thread.hpp:345
auto priority() const noexcept -> xtd::threading::thread_priority
Gets a value indicating the scheduling priority of a thread.
static auto join_all() -> void
Blocks the calling thread until all joinable threads terminate.
static const intptr invalid_thread_id
Represents an invalid native operating system thread id. This field is read-only.
Definition thread.hpp:68
auto thread_state() const noexcept -> xtd::threading::thread_state
Gets a value containing the states of the current thread.
static auto join_all(const collection_t &threads, const time_span &timeout) -> bool
Blocks the calling thread until all specified joinable threads collection terminate or the specified ...
Definition thread.hpp:359
auto auto_join() const noexcept -> bool
Gets a value indicating the current thread wiil be joined when destroyed.
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...
auto is_alive() const noexcept -> bool
Gets a value indicating the execution status of the current thread.
auto resume() -> void
Resumes a thread that has been suspended (Should not be used).
static auto sleep(const time_span &timeout) -> void
Suspends the current thread for a specified time.
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 ...
auto name() const noexcept -> string
Gets the name of the thread.
auto processor_affinity() const noexcept -> const xtd::array< xtd::usize > &
Gets the processors on which the associated thread can run.
auto thread_id() const noexcept -> intptr
Gets the native operating system thread id.
auto interrupt() -> void
Interrupts a thread that is in the xtd::threading::thread_state::wait_sleep_join thread state.
Contains a constant used to specify an infinite amount of time. This class cannot be inherited.
Definition timeout.hpp:33
static constexpr int32 infinite
A constant used to specify an infinite waiting period. This field is constant.
Definition timeout.hpp:41
Represents a time interval.
Definition time_span.hpp:29
Contains core_export_ keyword.
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
@ argument
The argument is not valid.
Definition exception_case.hpp:31
#define core_export_
Define shared library export.
Definition core_export.hpp:13
thread_priority
Specifies the scheduling priority of a system::threading::thread.
Definition thread_priority.hpp:28
xtd::shared_ptr_object< type_t > sptr
The xtd::sptr object is a shared pointer.
Definition sptr.hpp:25
std::intmax_t intptr
Represent a pointer or a handle.
Definition intptr.hpp:23
xtd::unique_ptr_object< type_t > uptr
The xtd::uptr object is a unique pointer.
Definition uptr.hpp:25
std::int32_t int32
Represents a 32-bit signed integer.
Definition int32.hpp:25
std::size_t usize
Represents an unsigned size of any object in bytes.
Definition usize.hpp:22
auto as(any_object &o) -> type_t
Casts a type into another type.
Definition __as_any_object.hpp:60
@ start
Starting of a logical operation.
Definition trace_event_type.hpp:37
Contains xtd::intptr type.
The xtd::threading::tasks namespace provides types that simplify the work of writing concurrent and a...
Definition itask.hpp:14
The xtd::threading namespace provides classes and interfaces that enable multithreaded programming....
Definition abandoned_mutex_exception.hpp:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
constexpr auto data() const noexcept -> const_pointer
Gets direct access to the underlying contiguous storage.
Definition read_only_span.hpp:197
Contains xtd::object class.
Contains xtd::threading::parameterized_thread_start delegate.
Contains xtd::threading::this_thread namespace.
Contains xtd::threading::thread_priority enumeration.
Contains xtd::threading::thread_start delegate.
Contains xtd::threading::thread_state enumeration.
Contains xtd::time_span class.
Contains xtd fundamental types.
Contains xtd::threading::wait_handle class.