glibmm: Glib::ThreadPool Class Reference

glibmm 2.54.1

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.

Deprecated:
  This is deprecated in favor of the standard C++ concurrency API in C++11 and C++14.

## Constructor & Destructor Documentation

## ◆ ThreadPool()

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.

## ◆ ~ThreadPool()

virtual Glib::ThreadPool::~ThreadPool | ( |  | ) |
| virtualnoexcept

## Member Function Documentation

## ◆ get_exclusive()

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

Returns whether all threads are exclusive to this pool.

Returns
  Whether all threads are exclusive to this pool.

## ◆ get_max_threads()

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

Returns the maximal number of threads for the pool.

Returns
  The maximal number of threads.

## ◆ get_max_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.

## ◆ get_num_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.

## ◆ get_num_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.

## ◆ gobj() [1/2]

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

## ◆ gobj() [2/2]

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

## ◆ push()

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.

## ◆ set_max_threads()

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.

## ◆ set_max_unused_threads()

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.

## ◆ shutdown()

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?

## ◆ stop_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().

## ◆ unprocessed()

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

Returns the number of tasks still unprocessed in the pool.

Returns
  The number of unprocessed tasks.