xtd 0.2.0
Loading...
Searching...
No Matches
thread_pool.h
Go to the documentation of this file.
1
4#pragma once
6#include "semaphore.h"
7#include "thread.h"
8#include "wait_callback.h"
10#include "../core_export.h"
11#include "../static.h"
12#include "../time_span.h"
13#include "../types.h"
14#include <vector>
15
17namespace xtd {
19 namespace threading {
53 friend class registered_wait_handle;
54
55 using asynchronous_io_thread_vector = std::vector<thread>;
56
57 struct static_data;
58
59 template<typename callback_t>
60 struct thread_item : public object {
61 thread_item() = default;
62 thread_item(const callback_t& callback) : callback(callback) {}
63 thread_item(const callback_t& callback, std::any state) : callback(callback), state(state) {}
64 thread_item(const callback_t& callback, std::any 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) {}
65
66 callback_t callback;
67 std::any state;
68 wait_handle* wait_object = null;
69 int32 milliseconds_timeout_interval;
70 bool execute_only_once = true;
71 bool unregistered = false;
72
73 void run() {
74 do {
75 this->callback(state);
76 } while (!execute_only_once);
77 }
78 };
79
80 using thread_pool_item = thread_item<wait_callback>;
81 using thread_pool_asynchronous_io_item = thread_item<wait_or_timer_callback>;
82 using thread_pool_item_collection = std::vector<thread_pool_item>;
83 using thread_pool_asynchronous_io_item_collection = std::vector<thread_pool_asynchronous_io_item>;
84
85 using thread_vector = std::vector<thread>;
86
87 public:
89
96 static void close();
97
102 static void get_available_threads(size_t& worker_threads, size_t& completion_port_threads);
103
110 static void get_max_threads(size_t& worker_threads, size_t& completion_port_threads);
111
115 static void get_min_threads(size_t& worker_threads, size_t& completion_port_threads);
116
122 static void join_all();
128 static bool join_all(int32 milliseconds_timeout);
134 static bool join_all(const time_span& timeout);
135
139 static bool queue_user_work_item(const wait_callback& callback);
144 static bool queue_user_work_item(const wait_callback& callback, std::any state);
145
147 template <typename callback_t>
148 static bool queue_user_work_item(callback_t callback) {return queue_user_work_item(wait_callback {callback});}
149 template <typename callback_t>
150 static bool queue_user_work_item(callback_t callback, std::any state) {return queue_user_work_item(wait_callback {callback}, state);}
152
161 static registered_wait_handle register_wait_for_single_object(wait_handle& wait_object, const wait_or_timer_callback& callback, std::any state, int32 milliseconds_timeout_interval, bool execute_only_once);
170 static registered_wait_handle register_wait_for_single_object(wait_handle& wait_object, const wait_or_timer_callback& callback, std::any state, int64 milliseconds_timeout_interval, bool execute_only_once);
179 static registered_wait_handle register_wait_for_single_object(wait_handle& wait_object, const wait_or_timer_callback& callback, std::any state, const time_span& timeout, bool execute_only_once);
188 static registered_wait_handle register_wait_for_single_object(wait_handle& wait_object, const wait_or_timer_callback& callback, std::any state, uint32 milliseconds_timeout_interval, bool execute_only_once);
189
191 template <typename callback_t>
192 static registered_wait_handle register_wait_for_single_object(wait_handle& wait_object, callback_t callback, std::any state, int32 milliseconds_timeout_interval, bool execute_only_once) {return register_wait_for_single_object(wait_object, wait_or_timer_callback {callback}, state, milliseconds_timeout_interval, execute_only_once);}
193 template <typename callback_t>
194 static registered_wait_handle register_wait_for_single_object(wait_handle& wait_object, callback_t callback, std::any state, int64 milliseconds_timeout_interval, bool execute_only_once) {return register_wait_for_single_object(wait_object, wait_or_timer_callback {callback}, state, milliseconds_timeout_interval, execute_only_once);}
195 template <typename callback_t>
196 static registered_wait_handle register_wait_for_single_object(wait_handle& wait_object, callback_t callback, std::any state, const time_span& timeout, bool execute_only_once) {return register_wait_for_single_object(wait_object, wait_or_timer_callback {callback}, state, timeout, execute_only_once);}
197 template <typename callback_t>
198 static registered_wait_handle register_wait_for_single_object(wait_handle& wait_object, callback_t callback, std::any state, uint32 milliseconds_timeout_interval, bool execute_only_once) {return register_wait_for_single_object(wait_object, wait_or_timer_callback {callback}, state, milliseconds_timeout_interval, execute_only_once);}
200
205 static bool set_max_threads(size_t worker_threads, size_t completion_port_threads);
206
211 static bool set_min_threads(size_t worker_threads, size_t completion_port_threads);
213
214 private:
215 friend class xtd::threading::thread;
216 static void asynchronous_io_run();
217 static void create_thread();
218 static void create_asynchronous_io_thread();
219 static void initialize_min_threads();
220 static void initialize_min_asynchronous_io_threads();
221 static bool join_all_threads(int32 milliseconds_timeout);
222 static bool join_all_asynchronous_io_threads(int32 milliseconds_timeout);
223 static void run();
224
225 static size_t max_threads_;
226 static size_t max_asynchronous_io_threads_;
227 static size_t min_threads_;
228 static size_t min_asynchronous_io_threads_;
229 static static_data static_data_;
230 };
231 }
232}
233
235// Add xtd::delegate::begin_invoke and xtd::delegate::end_invoke methods.
236#define __XTD_CORE_INTERNAL__
237#include "../internal/__delegate.h"
238#undef __XTD_CORE_INTERNAL__
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.h:42
A synchronization primitive that can also be used for interprocess synchronization.
Definition registered_wait_handle.h:29
Provides a pool of threads that can be used to execute tasks, post work items, process asynchronous I...
Definition thread_pool.h:52
static registered_wait_handle register_wait_for_single_object(wait_handle &wait_object, const wait_or_timer_callback &callback, std::any state, int32 milliseconds_timeout_interval, bool execute_only_once)
Registers a delegate to wait for a xtd::threading::wait_handle, specifying a 32-bit signed integer fo...
static bool set_max_threads(size_t worker_threads, size_t completion_port_threads)
Sets the number of requests to the thread pool that can be active concurrently. All requests above th...
static registered_wait_handle register_wait_for_single_object(wait_handle &wait_object, const wait_or_timer_callback &callback, std::any state, uint32 milliseconds_timeout_interval, bool execute_only_once)
Registers a delegate to wait for a xtd::threading::wait_handle, specifying a 32-bit signed integer fo...
static void join_all()
Join all resources and worker threads.
static bool join_all(int32 milliseconds_timeout)
Join all resources and worker threads.
static bool queue_user_work_item(const wait_callback &callback, std::any state)
Queues a method for execution. The method executes when a thread pool thread becomes available.
static bool set_min_threads(size_t worker_threads, size_t completion_port_threads)
Sets the number of idle threads the thread pool maintains in anticipation of new requests.
static bool join_all(const time_span &timeout)
Join all resources and worker threads.
static void close()
Close all resources and worker threads.
static bool queue_user_work_item(const wait_callback &callback)
Queues a method for execution. The method executes when a thread pool thread becomes available.
static registered_wait_handle register_wait_for_single_object(wait_handle &wait_object, const wait_or_timer_callback &callback, std::any state, const time_span &timeout, bool execute_only_once)
Registers a delegate to wait for a xtd::threading::wait_handle, specifying a 32-bit signed integer fo...
static void get_min_threads(size_t &worker_threads, size_t &completion_port_threads)
Retrieves the number of idle threads the thread pool maintains in anticipation of new requests....
static registered_wait_handle register_wait_for_single_object(wait_handle &wait_object, const wait_or_timer_callback &callback, std::any state, int64 milliseconds_timeout_interval, bool execute_only_once)
Registers a delegate to wait for a xtd::threading::wait_handle, specifying a 32-bit signed integer fo...
static void get_available_threads(size_t &worker_threads, size_t &completion_port_threads)
Retrieves the difference between the maximum number of thread pool threads returned by the GetMaxThre...
static void get_max_threads(size_t &worker_threads, size_t &completion_port_threads)
Retrieves the number of requests to the thread pool that can be active concurrently....
Creates and controls a thread, sets its priority, and gets its status.
Definition thread.h:43
Contains a constant used to specify an infinite amount of time. This class cannot be inherited.
Definition timeout.h:33
Encapsulates operating system specific objects that wait for exclusive access to shared resources.
Definition wait_handle.h:52
#define static_
This keyword is use to represent a static object. A static object can't be instantiated (constructors...
Definition static.h:37
#define core_export_
Define shared library export.
Definition core_export.h:13
int32_t int32
Represents a 32-bit signed integer.
Definition int32.h:23
int64_t int64
Represents a 64-bit signed integer.
Definition int64.h:23
null_ptr null
Represents a null pointer value.
uint32_t uint32
Represents a 32-bit unsigned integer.
Definition uint32.h:23
delegate< void(std::any)> wait_callback
Represents a callback method to be executed by a thread pool thread.
Definition wait_callback.h:30
delegate< void(std::any, bool)> 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.h:30
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Contains xtd::threading::registered_wait_handle exception.
Represents a time interval.
Definition time_span.h:29
Contains xtd::threading::semaphore exception.
Contains xtd::threading::wait_callback exception.
Contains xtd::threading::wait_or_timer_callback exception.
Contains xtd::threading::thread class.