glibmm 2.46.2

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Examples

- Class List
- Class Index
- Class Hierarchy
- Class Members

Public Member Functions | Static Public Member Functions | List of all members
Glib::ThreadPool Class Reference
Thread Pools

A pool of threads to execute work concurrently. More...

`#include <glibmm/threadpool.h>`

## Public Member Functions

| ThreadPool (int max_threads=-1, bool exclusive=false)
| Constructs a new thread pool. More...
virtual | ~ThreadPool () noexcept
void | push (const sigc::slot< void >& slot)
| Inserts slot into the list of tasks to be executed by the pool. More...
void | set_max_threads (int max_threads)
| Sets the maximal allowed number of threads for the pool. More...
int | get_max_threads () const
| Returns the maximal number of threads for the pool. More...
unsigned int | get_num_threads () const
| Returns the number of threads currently running in the pool. More...
unsigned int | unprocessed () const
| Returns the number of tasks still unprocessed in the pool. More...
bool | get_exclusive () const
| Returns whether all threads are exclusive to this pool. More...
void | shutdown (bool immediately=false)
| Frees all resources allocated for the pool. More...
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. More...
static int | get_max_unused_threads ()
| Returns the maximal allowed number of unused threads. More...
static unsigned int | get_num_unused_threads ()
| Returns the number of currently unused threads. More...
static void | stop_unused_threads ()
| Stops all currently unused threads. More...

## 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 noexcept

## Member Function Documentation

bool Glib::ThreadPool::get_exclusive | ( |  | ) | const

Returns whether all threads are exclusive to this pool.

Returns
  Whether all threads are exclusive to this pool.

int Glib::ThreadPool::get_max_threads | ( |  | ) | const

Returns the maximal number of threads for the pool.

Returns
  The maximal number of 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.

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.

static unsigned int Glib::ThreadPool::get_num_unused_threads | ( |  | ) |

static

Returns the number of currently unused threads.

Returns
  The number of currently unused threads.

GThreadPool* Glib::ThreadPool::gobj | ( |  | ) |

inline

const GThreadPool* Glib::ThreadPool::gobj | ( |  | ) | const

inline

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.

Because sigc::trackable is not thread-safe, if the slot represents a non-static class method and is created by sigc::mem_fun(), the class concerned should not derive from sigc::trackable. You can use, say, boost::bind() or, in C++11, std::bind() or a C++11 lambda expression instead of sigc::mem_fun().

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.

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.

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::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().

unsigned int Glib::ThreadPool::unprocessed | ( |  | ) | const

Returns the number of tasks still unprocessed in the pool.

Returns
  The number of unprocessed tasks.