xtd 0.2.0
Loading...
Searching...
No Matches
thread_pool.hpp
Go to the documentation of this file.
1
4#pragma once
6#include "semaphore.hpp"
7#include "thread.hpp"
8#include "wait_callback.hpp"
10#include "../core_export.hpp"
11#include "../new_ptr.hpp"
12#include "../static.hpp"
13#include "../time_span.hpp"
14#include "../types.hpp"
15
17namespace xtd {
19 namespace threading {
53 friend class registered_wait_handle;
54
55 struct static_data;
56
57 template<class callback_t>
58 class thread_item : public object {
59 struct sdata {
60 sdata() = default;
61 sdata(sdata&&) = default;
62 sdata(const sdata&) = default;
63 sdata& operator =(sdata&&) = default;
64 sdata& operator =(const sdata&) = default;
65 sdata(const callback_t& callback) : callback {callback} {}
66 sdata(const callback_t& callback, const xtd::any_object& state) : callback {callback}, state {state} {}
67 sdata(const callback_t& callback, const xtd::any_object& state, wait_handle* wait_object, int32 milliseconds_timeout_interval, bool execute_only_once) : callback {callback}, state {state}, wait_object {wait_object}, milliseconds_timeout_interval {milliseconds_timeout_interval}, execute_only_once {execute_only_once} {}
68
69 callback_t callback;
70 xtd::any_object state;
71 wait_handle* wait_object = null;
72 int32 milliseconds_timeout_interval;
73 bool execute_only_once = true;
74 bool unregistered = false;
75 };
76
77 public:
78 thread_item() = default;
79 thread_item(thread_item&&) = default;
80 thread_item(const thread_item& other) {*data = *other.data;}
81 thread_item& operator =(thread_item&&) = default;
82 thread_item& operator =(const thread_item& other) {*data = *other.data; return *this;}
83 thread_item(const callback_t& callback) : data {xtd::new_ptr<sdata>(callback)} {}
84 thread_item(const callback_t& callback, const xtd::any_object& state) : data {xtd::new_ptr<sdata>(callback, state)} {}
85 thread_item(const callback_t& callback, const xtd::any_object& state, wait_handle& wait_object, int32 milliseconds_timeout_interval, bool execute_only_once) : data {xtd::new_ptr<sdata>(callback, state, &wait_object, milliseconds_timeout_interval, execute_only_once)} {}
86
87 [[nodiscard]] auto execute_only_once() const noexcept -> bool {return data->execute_only_once;}
88 [[nodiscard]] auto milliseconds_timeout_interval() const noexcept -> int32 {return data->milliseconds_timeout_interval;}
89 [[nodiscard]] auto unregistered() const noexcept -> bool {return data->unregistered;}
90 auto unregistered(bool value) noexcept -> void {data->unregistered = value;}
91 auto wait_object() noexcept -> wait_handle* {return data->wait_object;}
92
93 auto run() -> void {data->callback(data->state);}
94 auto run(bool timeout) -> void {data->callback(data->state, timeout);}
95
96 private:
98 };
99
100 using thread_pool_item = thread_item<wait_callback>;
101 using thread_pool_asynchronous_io_item = thread_item<wait_or_timer_callback>;
102 using thread_pool_item_collection = xtd::collections::generic::list<thread_pool_item>;
103 using thread_pool_asynchronous_io_item_collection = xtd::collections::generic::list<thread_pool_asynchronous_io_item>;
104
105 public:
107
114 static auto close() -> void;
115
120 static auto get_available_threads(size_t& worker_threads, size_t& completion_port_threads) -> void;
121
128 static auto get_max_threads(size_t& worker_threads, size_t& completion_port_threads) -> void;
129
133 static auto get_min_threads(size_t& worker_threads, size_t& completion_port_threads) -> void;
134
140 static auto join_all() -> void;
146 static auto join_all(int32 milliseconds_timeout) -> bool;
152 static auto join_all(const time_span& timeout) -> bool;
153
156 static auto queue_user_work_item(const wait_callback& callback) -> void;
160 static auto queue_user_work_item(const wait_callback& callback, const xtd::any_object& state) -> void;
161
163 template<class callback_t>
164 static auto queue_user_work_item(callback_t callback) -> void {queue_user_work_item(wait_callback {callback});}
165 template<class callback_t>
166 static auto queue_user_work_item(callback_t callback, const xtd::any_object& state) -> void {queue_user_work_item(wait_callback {callback}, state);}
168
177 static auto register_wait_for_single_object(wait_handle& wait_object, const wait_or_timer_callback& callback, const xtd::any_object& state, int32 milliseconds_timeout_interval, bool execute_only_once) -> registered_wait_handle;
186 static auto register_wait_for_single_object(wait_handle& wait_object, const wait_or_timer_callback& callback, const xtd::any_object& state, int64 milliseconds_timeout_interval, bool execute_only_once) -> registered_wait_handle;
195 static auto register_wait_for_single_object(wait_handle& wait_object, const wait_or_timer_callback& callback, const xtd::any_object& state, const time_span& timeout, bool execute_only_once) -> registered_wait_handle;
204 static auto register_wait_for_single_object(wait_handle& wait_object, const wait_or_timer_callback& callback, const xtd::any_object& state, uint32 milliseconds_timeout_interval, bool execute_only_once) -> registered_wait_handle;
205
207 template<class callback_t>
208 static auto register_wait_for_single_object(wait_handle& wait_object, callback_t callback, const xtd::any_object& state, int32 milliseconds_timeout_interval, bool execute_only_once) -> registered_wait_handle {return register_wait_for_single_object(wait_object, wait_or_timer_callback {callback}, state, milliseconds_timeout_interval, execute_only_once);}
209 template<class callback_t>
210 static auto register_wait_for_single_object(wait_handle& wait_object, callback_t callback, const xtd::any_object& state, int64 milliseconds_timeout_interval, bool execute_only_once) -> registered_wait_handle {return register_wait_for_single_object(wait_object, wait_or_timer_callback {callback}, state, milliseconds_timeout_interval, execute_only_once);}
211 template<class callback_t>
212 static auto register_wait_for_single_object(wait_handle& wait_object, callback_t callback, const xtd::any_object& state, const time_span& timeout, bool execute_only_once) -> registered_wait_handle {return register_wait_for_single_object(wait_object, wait_or_timer_callback {callback}, state, timeout, execute_only_once);}
213 template<class callback_t>
214 static auto register_wait_for_single_object(wait_handle& wait_object, callback_t callback, const xtd::any_object& state, uint32 milliseconds_timeout_interval, bool execute_only_once) -> registered_wait_handle {return register_wait_for_single_object(wait_object, wait_or_timer_callback {callback}, state, milliseconds_timeout_interval, execute_only_once);}
216
221 static auto set_max_threads(size_t worker_threads, size_t completion_port_threads) -> bool;
222
227 static auto set_min_threads(size_t worker_threads, size_t completion_port_threads) -> bool;
229
230 private:
231 friend class xtd::threading::thread;
232 static auto asynchronous_io_run() -> void;
233 static auto create_thread() -> void;
234 static auto create_asynchronous_io_thread() -> void;
235 static auto initialize_min_threads() -> void;
236 static auto initialize_min_asynchronous_io_threads() -> void;
237 static auto join_all_threads(int32 milliseconds_timeout) -> bool;
238 static auto join_all_asynchronous_io_threads(int32 milliseconds_timeout) -> bool;
239 static auto run() -> void;
240
241 static size_t max_threads_;
242 static size_t max_asynchronous_io_threads_;
243 static size_t min_threads_;
244 static size_t min_asynchronous_io_threads_;
245 static static_data static_data_;
246 };
247 }
248}
249
251// Add xtd::delegate::begin_invoke and xtd::delegate::end_invoke methods.
252#define __XTD_CORE_INTERNAL__
254#undef __XTD_CORE_INTERNAL__
Represent a polymorphic wrapper capable of holding any type.
Definition any_object.hpp:29
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.hpp:80
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:45
A synchronization primitive that can also be used for interprocess synchronization.
Definition registered_wait_handle.hpp:29
Provides a pool of threads that can be used to execute tasks, post work items, process asynchronous I...
Definition thread_pool.hpp:52
static auto close() -> void
Close all resources and worker threads.
static auto get_min_threads(size_t &worker_threads, size_t &completion_port_threads) -> void
Retrieves the number of idle threads the thread pool maintains in anticipation of new requests....
static auto register_wait_for_single_object(wait_handle &wait_object, const wait_or_timer_callback &callback, const xtd::any_object &state, int32 milliseconds_timeout_interval, bool execute_only_once) -> registered_wait_handle
Registers a delegate to wait for a xtd::threading::wait_handle, specifying a 32-bit signed integer fo...
static auto get_max_threads(size_t &worker_threads, size_t &completion_port_threads) -> void
Retrieves the number of requests to the thread pool that can be active concurrently....
static auto get_available_threads(size_t &worker_threads, size_t &completion_port_threads) -> void
Retrieves the difference between the maximum number of thread pool threads returned by the GetMaxThre...
static auto queue_user_work_item(const wait_callback &callback) -> void
Queues a method for execution. The method executes when a thread pool thread becomes available.
static auto join_all(const time_span &timeout) -> bool
Join all resources and worker threads.
static auto join_all(int32 milliseconds_timeout) -> bool
Join all resources and worker threads.
static auto register_wait_for_single_object(wait_handle &wait_object, const wait_or_timer_callback &callback, const xtd::any_object &state, uint32 milliseconds_timeout_interval, bool execute_only_once) -> registered_wait_handle
Registers a delegate to wait for a xtd::threading::wait_handle, specifying a 32-bit signed integer fo...
static auto register_wait_for_single_object(wait_handle &wait_object, const wait_or_timer_callback &callback, const xtd::any_object &state, int64 milliseconds_timeout_interval, bool execute_only_once) -> registered_wait_handle
Registers a delegate to wait for a xtd::threading::wait_handle, specifying a 32-bit signed integer fo...
static auto set_max_threads(size_t worker_threads, size_t completion_port_threads) -> bool
Sets the number of requests to the thread pool that can be active concurrently. All requests above th...
static auto queue_user_work_item(const wait_callback &callback, const xtd::any_object &state) -> void
Queues a method for execution. The method executes when a thread pool thread becomes available.
static auto register_wait_for_single_object(wait_handle &wait_object, const wait_or_timer_callback &callback, const xtd::any_object &state, const time_span &timeout, bool execute_only_once) -> registered_wait_handle
Registers a delegate to wait for a xtd::threading::wait_handle, specifying a 32-bit signed integer fo...
static auto join_all() -> void
Join all resources and worker threads.
static auto set_min_threads(size_t worker_threads, size_t completion_port_threads) -> bool
Sets the number of idle threads the thread pool maintains in anticipation of new requests.
Creates and controls a thread, sets its priority, and gets its status.
Definition thread.hpp:49
Contains a constant used to specify an infinite amount of time. This class cannot be inherited.
Definition timeout.hpp:33
Encapsulates operating system specific objects that wait for exclusive access to shared resources.
Definition wait_handle.hpp:52
Represents a time interval.
Definition time_span.hpp:29
Contains core_export_ keyword.
xtd::delegate< void(const xtd::any_object &state, bool timed_out)> wait_or_timer_callback
Represents a method to be called when a xtd::threading::wait_handle is signaled or times out.
Definition wait_or_timer_callback.hpp:30
xtd::delegate< void(const xtd::any_object &state)> wait_callback
Represents a callback method to be executed by a thread pool thread.
Definition wait_callback.hpp:30
#define static_
This keyword is use to represent a static object. A static object can't be instantiated (constructors...
Definition static.hpp:38
#define core_export_
Define shared library export.
Definition core_export.hpp:13
std::int64_t int64
Represents a 64-bit signed integer.
Definition int64.hpp:23
std::uint32_t uint32
Represents a 32-bit unsigned integer.
Definition uint32.hpp:23
null_ptr null
Represents a null pointer value.
std::int32_t int32
Represents a 32-bit signed integer.
Definition int32.hpp:23
xtd::sptr< type_t > ptr
The xtd::ptr object is a shared pointer.
Definition ptr.hpp:27
ptr< type_t > new_ptr(args_t &&... args)
The xtd::new_ptr operator creates a xtd::ptr object.
Definition new_ptr.hpp:24
@ other
The operating system is other.
Definition platform_id.hpp:60
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 const_pointer data() const noexcept
Gets direct access to the underlying contiguous storage.
Definition read_only_span.hpp:201
Contains xtd::new_ptr method.
Contains xtd::threading::registered_wait_handle exception.
Contains xtd::threading::semaphore class.
Contains xtd::static_object class.
Contains xtd::threading::thread class.
Contains xtd::time_span class.
Contains xtd fundamental types.
Contains xtd::threading::wait_callback delegate.
Contains xtd::threading::wait_or_timer_callback delegate.