|
c++-gtk-utils
|
A thread-pool class for managing tasks in multi-threaded programs. More...
#include <c++-gtk-utils/task_manager.h>
Public Types | |
| enum | StopMode { wait_for_running, wait_for_all } |
Public Member Functions | |
| TaskManager (const TaskManager &) | |
| TaskManager & | operator= (const TaskManager &) |
| unsigned int | get_max_threads () const |
| unsigned int | get_min_threads () const |
| unsigned int | get_used_threads () const |
| unsigned int | get_tasks () const |
| void | set_max_threads (unsigned int max) |
| unsigned int | get_idle_time () const |
| void | set_idle_time (unsigned int idle) |
| bool | get_blocking () const |
| void | set_blocking (bool blocking) |
| StopMode | get_stop_mode () const |
| void | set_stop_mode (StopMode mode) |
| void | stop_all () |
| void | add_task (const Callback::Callback *task) |
| void | add_task (std::unique_ptr< const Callback::Callback > task, std::unique_ptr< const Callback::Callback > fail) |
| bool | is_error () const |
| TaskManager (unsigned int max=8, unsigned int min=0, unsigned int idle=10000, bool blocking=true, StopMode mode=TaskManager::wait_for_all) | |
| ~TaskManager () | |
A thread-pool class for managing tasks in multi-threaded programs.
Cgu::Thread::Future operates on the principle of there being one worker thread per task. In some cases however, it may be better to have a limited pool of worker threads executing a larger number of tasks. This class implements this approach via a thread pool.
One common approach for thread pools of this kind is to set the maximum number of threads to the number of cores, or one less than the number of cores, available on the local machine. How that can be determined is system specific (on linux it can be obtained by, for example, inspecting the 'siblings' and 'cpu cores' fields in /proc/cpuinfo or by using sysconf with the glibc extension for _SC_NPROCESSORS_ONLN).
Where the task needs to provide a result, two approaches can be adopted. First, the task callback can have a Cgu::AsyncResult object held by Cgu::SharedLockPtr (or by std::shared_ptr having a thread safe reference count) bound to it. Alternatively, a task can provide a result asynchronously to a glib main loop by calling Cgu::Callback::post() when it is ready to do so. In addition, tasks can add other tasks, enabling the composition of an arbitrary number of tasks to obtain a final result.
TaskManager objects do not provide thread cancellation. Thread cancellation is incompatible with the task-centred thread pool model. If task cancellation is wanted, use a Cgu::Thread::Future (or Cgu::Thread::Thread or Cgu::Thread::JoinableHandle) object instead, and have a dedicated thread for the cancelable task.
If glib < 2.32 is installed, g_thread_init() must be called before any TaskManager objects are constructed, which in turn means that with glib < 2.32 TaskManager objects may not be constructed as static objects in global namespace (that is, before g_thread_init() has been called in the program).
Any exceptions which propagate from a task will be consumed to protect the TaskManager object, and to detect whether this has happened there is a version of the TaskManager::add_task() method which takes a second argument comprising a 'fail' callback. If an exception propagates from the 'fail' callback that is also consumed and a g_critical() message issued.
Tasks can be aborted by throwing Cgu::Thread::Exit (as well as any other exception). Where a thread is managed by a TaskManager object, throwing Cgu::Thread::Exit will only terminate the task and not the thread on which it is running (and will cause the 'fail' callback to be executed, if there is one).
TaskManager objects have no copy constructor or copy assignment operator, as copying them would have no obvious semantic meaning. Whilst swapping or moving TaskManager objects would be meaningful, this is not implemented either because it would require an additional internal lock to be thread safe, and the circumstances in which moving or swapping would be useful are limited. Where a move option is wanted, a TaskManager object can be constructed on free store and held by std::unique_ptr.
Here is a compilable example of the calculator class referred to in the documentation on the AsyncResult but which uses a TaskManager object so that the calculator class can run more than one thread to service its calculations:
| Cgu::Thread::TaskManager::TaskManager | ( | const TaskManager & | ) |
This class cannot be copied. The copy constructor is deleted.
| Cgu::Thread::TaskManager::TaskManager | ( | unsigned int | max = 8, |
| unsigned int | min = 0, |
||
| unsigned int | idle = 10000, |
||
| bool | blocking = true, |
||
| StopMode | mode = TaskManager::wait_for_all |
||
| ) |
If the specified minimum number of threads is greater than 0, this constructor will start the required minimum number of threads. If glib < 2.32 is installed, g_thread_init() must be called before any TaskManager objects are constructed
| max | The maximum number of threads which the TaskManager object will run in the thread pool. If the value passed as this argument is less than the value passed as 'min', the maximum number of threads will be set to 'min'. A value of 0 is not valid, and if this is passed the number will be set to the greater of 1 and 'min'. |
| min | The minimum number of threads which the TaskManager object will run in the thread pool. |
| idle | The length of time in milliseconds that threads greater in number than 'min' and not executing any tasks will remain in existence. The default is 10000 (10 seconds). |
| blocking | If true, calls to stop_all() and the destructor will not return until the tasks remaining to be executed have finished (what is mean by "the tasks remaining to be executed" depends on the StopMode setting, for which see the documentation on the stop_all() method). If false, stop_all() and the destructor will return straight away (which in terms of the TaskManager class implementation is safe for the reasons explained in the documentation on the destructor). |
| mode | The StopMode setting (either Cgu::Thread::TaskManager::wait_for_running or Cgu::Thread::TaskManager::wait_for_all) executed when running stop_all() or when the destructor is called. See the documentation on stop_all() for an explanation of the setting. |
| std::bad_alloc | This exception might be thrown if memory is exhausted and the system throws in that case. |
| Cgu::Thread::TaskError | This exception will be thrown if starting the specified minimum number of threads fails. |
| Cgu::Thread::MutexError | This exception might be thrown if initialisation of the contained mutex fails. (It is often not worth checking for this, as it means either memory is exhausted or pthread has run out of other resources to create new mutexes.) |
| Cgu::Thread::CondError | This exception might be thrown if initialisation of the contained condition variable fails. (It is often not worth checking for this, as it means either memory is exhausted or pthread has run out of other resources to create new condition variables.) |
Since 2.0.12
| Cgu::Thread::TaskManager::~TaskManager | ( | ) |
The destructor will call stop_all(), unless that method has previously been called explicitly without throwing std::bad_alloc. If the blocking setting is true, the destructor will not return until the tasks remaining to be executed have finished (what is mean by "the tasks remaining to be executed" depends on the StopMode setting, for which see the documentation on the stop_all() method.) If the blocking setting is false, the destructor will return straight away: this is safe, because TaskManager's internals for running tasks have been implemented using reference counting and will not be deleted until all threads running on the TaskManager object have finished, although the remaining tasks should not attempt to call any of TaskManager's methods once the TaskManager object itself has been destroyed.
The destructor is thread safe (any thread can destroy a TaskManager object) unless the blocking setting is true, in which case no task running on the TaskManager object may destroy the TaskManager object. Subject to that, it is not an error for a thread to destroy a TaskManager object and so invoke this destructor while another thread is already blocking in (if the blocking setting is true) or already out of (if the blocking setting is false) a call to stop_all() and remaining tasks are executing: if blocking, both calls (to stop_all() and to this destructor) would safely block together. Any given thread can similarly safely follow a non-blocking call to stop_all() by a non-blocking call to this destructor even though remaining tasks are executing. However, it is an error for a thread to call stop_all() after another thread has begun destruction of the TaskManager object (that is, after this destructor has been entered): there would then be an unresolvable race with the destructor.
The destructor will not throw.
If stop_all() has not previously been called explicitly and throws std::bad_alloc() when called in this destructor, the exception will be caught and consumed, but then the destructor will not block even if the blocking setting is true, and if the minimum number of threads is not 0 some threads might remain running during the entire program duration (albeit safely). Where the throwing of std::bad_alloc is a meaningful event (usually it isn't) and needs to be guarded against, call stop_all() explicitly before this destructor is entered, or use a minimum thread value of 0 and allow for the case of the destructor not blocking.
Since 2.0.12
|
inline |
This method adds a new task. If one or more threads in the pool are currently blocking and waiting for a task, then the task will begin executing immediately in one of the threads. If not, and the value returned by get_used_threads() is less than the value returned by get_max_threads(), a new thread will start and the task will execute immediately in the new thread. Otherwise, the task will be queued for execution as soon as a thread becomes available. Tasks will be executed in the order in which they are added to the ThreadManager object. This method is thread safe (any thread may call it, including any task running on the TaskManager object).
A task may terminate itself prematurely by throwing Cgu::Thread::Exit. In addition, the implementation of TaskManager will consume any other exception escaping from the task callback and safely terminate the task concerned in order to protect the integrity of the TaskManager object. Where detecting any of these outcomes is important (usually it won't be), the two argument version of this method is available so that a 'fail' callback can be executed in these circumstances.
| task | A callback representing the new task, as constructed by the Callback::make(), Callback::make_ref() or Callback::lambda() factory functions. Ownership is taken of this callback, and it will be disposed of when it has been finished with. The destructors of any bound arguments in the callback must not throw. |
| std::bad_alloc | This exception will be thrown if memory is exhausted and the sytem throws in that case. (On systems with over-commit/lazy-commit combined with virtual memory (swap), it is rarely useful to check for memory exhaustion). If this exception is thrown, the 'task' callback will be disposed of. |
| Cgu::Thread::TaskError | This exception will be thrown if stop_all() has previously been called. It will also be thrown if is_error() would return true because this class's internal thread pool loop implementation has thrown std::bad_alloc, or a thread has failed to start correctly. (On systems with over-commit/lazy-commit combined with virtual memory (swap), it is rarely useful to check for memory exhaustion, but there may be some specialized cases where the return value of is_error() is useful.) If this exception is thrown, the 'task' callback will be disposed of. |
Since 2.0.12
| void Cgu::Thread::TaskManager::add_task | ( | std::unique_ptr< const Callback::Callback > | task, |
| std::unique_ptr< const Callback::Callback > | fail | ||
| ) |
This method adds a new task. If one or more threads in the pool are currently blocking and waiting for a task, then the task will begin executing immediately in one of the threads. If not, and the value returned by get_used_threads() is less than the value returned by get_max_threads(), a new thread will start and the task will execute immediately in the new thread. Otherwise, the task will be queued for execution as soon as a thread becomes available. Tasks will be executed in the order in which they are added to the ThreadManager object. This method is thread safe (any thread may call it, including any task running on the TaskManager object).
A task may terminate itself prematurely by throwing Cgu::Thread::Exit. In addition, the implementation of TaskManager will consume any other exception escaping from the task callback and safely terminate the task concerned in order to protect the integrity of the TaskManager object. Where detecting any of these outcomes is important (usually it won't be), a callback can be passed to the 'fail' argument which will execute if, and only if, either Cgu::Thread::Exit is thrown or some other exception has propagated from the task. This 'fail' callback is different from the 'fail' callback of Cgu::Thread::Future objects (programming for many tasks to a lesser number of threads requires different approaches from programming for one thread per task), and it executes in the task thread rather than executing in a glib main loop (however, the 'fail' callback can of course call Cgu::Callback::post() to execute another callback in a main loop, if that is what is wanted).
| task | A callback representing the new task, as constructed by the Callback::make(), Callback::make_ref() or Callback::lambda() factory functions. |
| fail | A callback which will be executed if the function executed by the 'task' callback exits by throwing Thread::Exit or some other exception. If an exception propagates from the function represented by this callback, this will be consumed to protect the TaskManager object, and a g_critical() warning will be issued. |
| std::bad_alloc | This exception will be thrown if memory is exhausted and the sytem throws in that case. (On systems with over-commit/lazy-commit combined with virtual memory (swap), it is rarely useful to check for memory exhaustion). |
| Cgu::Thread::TaskError | This exception will be thrown if stop_all() has previously been called. It will also be thrown if is_error() would return true because this class's internal thread pool loop implementation has thrown std::bad_alloc, or a thread has failed to start correctly. (On systems with over-commit/lazy-commit combined with virtual memory (swap), it is rarely useful to check for memory exhaustion, but there may be some specialized cases where the return value of is_error() is useful.) |
Since 2.0.12
| bool Cgu::Thread::TaskManager::get_blocking | ( | ) | const |
Gets the current blocking setting, which determines whether calls to stop_all() and the destructor will block waiting for all remaining tasks to complete. This value is established initially by the 'blocking' argument passed to the TaskManager constructor and can subequently be changed by calling set_blocking(). This method will not throw and is thread safe.
Since 2.0.12
| unsigned int Cgu::Thread::TaskManager::get_idle_time | ( | ) | const |
Gets the length of time in milliseconds that threads greater in number than the minimum and not executing any tasks will remain in existence waiting for new tasks. This value is established initially by the 'idle' argument passed to the TaskManager constructor and can subequently be changed by calling set_idle_time(). The default value is 10000 (10 seconds). This method will not throw and is thread safe.
Since 2.0.12
| unsigned int Cgu::Thread::TaskManager::get_max_threads | ( | ) | const |
Gets the maximum number of threads which the TaskManager object is currently set to run in the thread pool. This value is established initially by the 'max' argument passed to the TaskManager constructor and can subequently be changed by calling set_max_threads(). The default value is 8. This method will not throw and is thread safe.
Since 2.0.12
| unsigned int Cgu::Thread::TaskManager::get_min_threads | ( | ) | const |
Gets the minimum number of threads which the TaskManager object will run in the thread pool (these threads will last until stop_all() is called or the TaskManager object is destroyed). This value is established by the 'min' argument passed to the TaskManager constructor and cannot subequently be changed. The default is 0. This method will not throw and is thread safe.
Since 2.0.12
| StopMode Cgu::Thread::TaskManager::get_stop_mode | ( | ) | const |
Gets the current StopMode setting (either Cgu::Thread::TaskManager::wait_for_running or Cgu::Thread::TaskManager::wait_for_all) executed when running stop_all() or when the destructor is called. See the documentation on stop_all() for an explanation of the setting. This value is established initially by the 'mode' argument passed to the TaskManager constructor and can subequently be changed by calling set_stop_mode(). This method will not throw and is thread safe.
Since 2.0.12
| unsigned int Cgu::Thread::TaskManager::get_tasks | ( | ) | const |
Gets the number of tasks which the TaskManager object is at present either running in the thread pool or has queued for execution. This value will be less than the number returned by get_used_threads() if threads in the thread pool are currently waiting to receive tasks for execution. This method will not throw and is thread safe.
Since 2.0.12
| unsigned int Cgu::Thread::TaskManager::get_used_threads | ( | ) | const |
Gets the number of threads which the TaskManager object is currently running in the thread pool. This value could be greater than the number returned by get_max_threads() if set_max_threads() has recently been called with a value which is less than that number but not enough tasks have since completed to reduce the number of running threads to the new value set. This method will not throw and is thread safe.
Since 2.0.12
| bool Cgu::Thread::TaskManager::is_error | ( | ) | const |
This will return true if a thread required by the thread pool has failed to start correctly because of memory exhaustion or because pthread has run out of other resources to start new threads, or because an internal operation has thrown std::bad_alloc. (On systems with over-commit/lazy-commit combined with virtual memory (swap), it is rarely useful to check for memory exhaustion, and even more so where glib is used, as that terminates a program if memory cannot be obtained from the operating system, but there may be some specialized cases where the return value of this method is useful - this class does not use any glib functions which might cause such termination.) This method will not throw and is thread safe.
Since 2.0.12
| TaskManager& Cgu::Thread::TaskManager::operator= | ( | const TaskManager & | ) |
This class cannot be copied. The assignment operator is deleted.
| void Cgu::Thread::TaskManager::set_blocking | ( | bool | blocking | ) |
Sets the current blocking setting, which determines whether calls to stop_all() and the destructor will block waiting for all remaining tasks to complete. This method cannot be called after stop_all() has been called (if that is attempted, Cgu::Thread::TaskError will be thrown). It is thread safe.
| blocking | The new blocking setting. |
| Cgu::Thread::TaskError | This exception will be thrown if stop_all() has previously been called. |
Since 2.0.12
| void Cgu::Thread::TaskManager::set_idle_time | ( | unsigned int | idle | ) |
Sets the length of time in milliseconds that threads greater in number than the minimum and not executing any tasks will remain in existence waiting for new tasks. This will only have effect for threads in the pool which begin waiting for new tasks after this method is called. This method will not throw and is thread safe.
| idle | The length of the idle time in milliseconds during which threads will remain waiting for new tasks. |
Since 2.0.12
| void Cgu::Thread::TaskManager::set_max_threads | ( | unsigned int | max | ) |
Sets the maximum number of threads which the TaskManager object will currently run in the thread pool. If this is less than the current number of running threads, the number of threads actually running will only be reduced as tasks complete, or as idle timeouts expire. This method does nothing if stop_all() has previously been called. This method is thread safe.
| max | The maximum number of threads which the TaskManager object will currently run in the thread pool. This method will not set the maximum value of threads to a value less than that returned by get_min_threads(). |
| std::bad_alloc | If this call is passed a value for 'max' which increases the maximum number of threads from its previous setting and tasks are currently queued for execution, new threads will be started for the queued tasks, so this exception may be thrown on starting the new threads if memory is exhausted and the system throws in that case. (On systems with over-commit/lazy-commit combined with virtual memory (swap), it is rarely useful to check for memory exhaustion). |
| Cgu::Thread::TaskError | If this call is passed a value for 'max' which increases the maximum number of threads from its previous setting and tasks are currently queued for execution, new threads will be started for the queued tasks, so this exception may be thrown on starting the new threads if a thread fails to start correctly (this would mean that memory is exhausted, the pthread thread limit has been reached or pthread has run out of other resources to start new threads). |
Since 2.0.12
| void Cgu::Thread::TaskManager::set_stop_mode | ( | StopMode | mode | ) |
Sets the current StopMode setting (either Cgu::Thread::TaskManager::wait_for_running or Cgu::Thread::TaskManager::wait_for_all) executed when running stop_all() or when the destructor is called. See the documentation on stop_all() for an explanation of the setting. This method will not throw and is thread safe.
| mode | The new StopMode setting. |
Since 2.0.12
| void Cgu::Thread::TaskManager::stop_all | ( | ) |
This will cause the TaskManager object to stop running tasks. The precise effect depends on the current StopMode and blocking settings. If StopMode is set to Cgu::Thread::TaskManager::wait_for_running, all queued tasks which are not yet running on a thread will be dispensed with, but any already running will be left to complete normally. If StopMode is set to Cgu::Thread::TaskManager::wait_for_all, both already running tasks and all tasks already queued will be permitted to execute and complete normally. If the blocking setting is set to true, this method will wait until all the tasks still to execute have finished before returning, and if false it will return straight away.
After this method has been called, any attempt to add further tasks with the add_task() method will fail, and add_task() will throw Cgu::Thread::TaskError.
This method is thread safe (any thread may call it) unless the blocking setting is true, in which case no task running on the TaskManager object may call this method.
| std::bad_alloc | This exception will be thrown if memory is exhausted and the system throws in that case. (On systems with over-commit/lazy-commit combined with virtual memory (swap), it is rarely useful to check for memory exhaustion). |
| Cgu::Thread::TaskError | This exception will be thrown if stop_all() has previously been called, unless that previous call threw std::bad_alloc: if std::bad_alloc is thrown, this method may be called again to stop all threads, once the memory deficiency is dealt with, but no other methods of the TaskManager object should be called. |
Since 2.0.12
1.8.1.1