Glib::ThreadPool Class Reference
[Thread Pools]
A pool of threads to execute work concurrently.
More...
Public Member Functions | |
ThreadPool (int max_threads=-1, bool exclusive=false) | |
Constructs a new thread pool. | |
virtual | ~ThreadPool () |
void | push (const sigc::slot<void>& slot) |
Inserts slot into the list of tasks to be executed by the pool. | |
void | set_max_threads (int max_threads) |
Sets the maximal allowed number of threads for the pool. | |
int | get_max_threads () const |
Returns the maximal number of threads for the pool. | |
unsigned int | get_num_threads () const |
Returns the number of threads currently running in the pool. | |
unsigned int | unprocessed () const |
Returns the number of tasks still unprocessed in the pool. | |
bool | get_exclusive () const |
Returns whether all threads are exclusive to this pool. | |
void | shutdown (bool immediately=false) |
Frees all resources allocated for the pool. | |
GThreadPool* | gobj () |
const GThreadPool* | gobj () const |
Static Public Member Functions | |
static void | set_max_unused_threads (int max_threads) |
Sets the maximal number of unused threads to max_threads. | |
static int | get_max_unused_threads () |
Returns the maximal allowed number of unused threads. | |
static unsigned int | get_num_unused_threads () |
Returns the number of currently unused threads. | |
static void | stop_unused_threads () |
Stops all currently unused threads. |
Detailed Description
A pool of threads to execute work concurrently.
Constructor & Destructor Documentation
Glib::ThreadPool::ThreadPool | ( | int | max_threads = -1 , |
|
bool | exclusive = false | |||
) | [explicit] |
Constructs a new thread pool.
Whenever you call ThreadPool::push(), either a new thread is created or an unused one is reused. At most max_threads threads are running concurrently for this thread pool. max_threads = -1 allows unlimited threads to be created for this thread pool.
The parameter exclusive determines, whether the thread pool owns all threads exclusive or whether the threads are shared globally. If exclusive is true
, max_threads threads are started immediately and they will run exclusively for this thread pool until it is destroyed by ~ThreadPool(). If exclusive is false
, threads are created when needed and shared between all non-exclusive thread pools. This implies that max_threads may not be -1 for exclusive thread pools.
- Parameters:
-
max_threads The maximal number of threads to execute concurrently in the new thread pool, -1 means no limit. exclusive Should this thread pool be exclusive?
- Exceptions:
-
Glib::ThreadError An error can only occur when exclusive is set to true
and not all max_threads threads could be created.
virtual Glib::ThreadPool::~ThreadPool | ( | ) | [virtual] |
Member Function Documentation
void Glib::ThreadPool::push | ( | const sigc::slot< void > & | slot | ) |
Inserts slot into the list of tasks to be executed by the pool.
When the number of currently running threads is lower than the maximal allowed number of threads, a new thread is started (or reused). Otherwise slot stays in the queue until a thread in this pool finishes its previous task and processes slot.
- Parameters:
-
slot A new task for the thread pool.
- Exceptions:
-
Glib::ThreadError An error can only occur when a new thread couldn't be created. In that case slot is simply appended to the queue of work to do.
void Glib::ThreadPool::set_max_threads | ( | int | max_threads | ) |
Sets the maximal allowed number of threads for the pool.
A value of -1 means that the maximal number of threads is unlimited. Setting max_threads to 0 means stopping all work for pool. It is effectively frozen until max_threads is set to a non-zero value again.
A thread is never terminated while it is still running. Instead the maximal number of threads only has effect for the allocation of new threads in ThreadPool::push(). A new thread is allocated whenever the number of currently running threads in the pool is smaller than the maximal number.
- Parameters:
-
max_threads A new maximal number of threads for the pool.
- Exceptions:
-
Glib::ThreadError An error can only occur when a new thread couldn't be created.
int Glib::ThreadPool::get_max_threads | ( | ) | const |
Returns the maximal number of threads for the pool.
- Returns:
- The maximal number of threads.
unsigned int Glib::ThreadPool::get_num_threads | ( | ) | const |
Returns the number of threads currently running in the pool.
- Returns:
- The number of threads currently running.
unsigned int Glib::ThreadPool::unprocessed | ( | ) | const |
Returns the number of tasks still unprocessed in the pool.
- Returns:
- The number of unprocessed tasks.
bool Glib::ThreadPool::get_exclusive | ( | ) | const |
Returns whether all threads are exclusive to this pool.
- Returns:
- Whether all threads are exclusive to this pool.
void Glib::ThreadPool::shutdown | ( | bool | immediately = false |
) |
Frees all resources allocated for the pool.
If immediately is true
, no new task is processed. Otherwise the pool is not freed before the last task is processed. Note however, that no thread of this pool is interrupted while processing a task. Instead at least all still running threads can finish their tasks before the pool is freed.
This method does not return before all tasks to be processed (dependent on immediately, whether all or only the currently running) are ready. After calling shutdown() the pool must not be used anymore.
- Parameters:
-
immediately Should the pool shut down immediately?
static void Glib::ThreadPool::set_max_unused_threads | ( | int | max_threads | ) | [static] |
Sets the maximal number of unused threads to max_threads.
If max_threads is -1, no limit is imposed on the number of unused threads.
- Parameters:
-
max_threads Maximal number of unused threads.
static int Glib::ThreadPool::get_max_unused_threads | ( | ) | [static] |
Returns the maximal allowed number of unused threads.
- Returns:
- The maximal number of unused threads.
static unsigned int Glib::ThreadPool::get_num_unused_threads | ( | ) | [static] |
Returns the number of currently unused threads.
- Returns:
- The number of currently unused threads.
static void Glib::ThreadPool::stop_unused_threads | ( | ) | [static] |
Stops all currently unused threads.
This does not change the maximal number of unused threads. This function can be used to regularly stop all unused threads e.g. from Glib::signal_timeout().
GThreadPool* Glib::ThreadPool::gobj | ( | ) | [inline] |
const GThreadPool* Glib::ThreadPool::gobj | ( | ) | const [inline] |
The documentation for this class was generated from the following file: