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.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
- 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/threading/auto_reset_event>
#include <xtd/threading/thread_pool>
#include <xtd/console>
#include <xtd/date_time>
#include <xtd/random>
#include <xtd/startup>
namespace wait_handle_example {
class program {
public:
static void main() {
thread_pool::queue_user_work_item({thread_proc});
console::write_line("Main thread does some work, then sleeps.");
thread::sleep(1000);
console::write_line("Main thread exits.");
}
private:
static void thread_proc(std::any state_info) {
console::write_line("Hello from the thread pool.");
}
};
}
startup_(wait_handle_example::program::main);
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.h:175
The xtd::threading namespace provides classes and interfaces that enable multithreaded programming....
Definition abandoned_mutex_exception.h:11
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
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 void | close () |
| Close all resources and worker threads.
|
|
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 GetMaxThreads method, and the number currently active.
|
|
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. All requests above that number remain queued until thread pool threads become available.
|
|
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. Always 0 for both.
|
|
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 | join_all (const time_span &timeout) |
| Join 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 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 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 for the time-out in milliseconds.
|
|
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 for the time-out in milliseconds.
|
|
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 for the time-out in milliseconds.
|
|
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 for the time-out in milliseconds.
|
|
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 that number remain queued until thread pool threads become 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.
|
|