Provides a pool of threads that can be used to execute tasks, post work items, process asynchronous I/O, wait on behalf of other threads, and process timers.
Provides a pool of threads that can be used to execute tasks, post work items, process asynchronous I...
Definition thread_pool.hpp:52
#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
- Inheritance
- xtd::static_object → xtd::threading::thread_pool
- Header
#include <xtd/threading/thread_pool>
- Namespace
- xtd::threading
- Library
- xtd.core
- Examples
- In the following example, the main application thread queues a method named ThreadProc to execute on a thread pool thread, sleeps for one second, and then exits. The ThreadProc method simply displays a message.
#include <xtd/xtd>
namespace wait_handle_example {
class program {
public:
static void main() {
}
private:
}
};
}
startup_(wait_handle_example::program::main);
Represent a polymorphic wrapper capable of holding any type.
Definition any_object.hpp:29
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
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 sleep(int32 milliseconds_timeout) -> void
Suspends the current thread for a specified time.
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.hpp:284
If you comment out the call to the xtd::threading::thread::sleep method, the main thread exits before method runs on the thread pool thread. The thread pool uses background threads, which do not keep the application running if all foreground threads have terminated. (This is a simple example of a race condition.)
- Note
- The threads in the managed thread pool are background threads. That is, their xtd::threading::thread::is_background properties are true. This means that a xtd::threading::thread_pool thread will not keep an application running after all foreground threads have exited.
- Note
- When demand is low, the actual number of thread pool threads can fall below the minimum values.
- Warning
- You can use the xtd::threading::thread_pool::set_min_threads method to increase the minimum number of threads. However, unnecessarily increasing these values can cause performance problems. If too many tasks start at the same time, all of them might appear to be slow. In most cases the thread pool will perform better with its own algorithm for allocating threads.
|
| static auto | close () -> void |
| | Close all resources and worker threads.
|
| 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 GetMaxThreads method, and the number currently active.
|
| 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. All requests above that number remain queued until thread pool threads become available.
|
| 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. Always 0 for both.
|
| static auto | join_all () -> void |
| | Join all resources and worker threads.
|
| static auto | join_all (int32 milliseconds_timeout) -> bool |
| | Join all resources and worker threads.
|
| static auto | join_all (const time_span &timeout) -> bool |
| | Join all resources and worker threads.
|
| 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 | 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, 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 for the time-out in milliseconds.
|
| 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 for the time-out in milliseconds.
|
| 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 for the time-out in milliseconds.
|
| 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 for the time-out in milliseconds.
|
| 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 that number remain queued until thread pool threads become available.
|
| 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.
|