Xfce Foundation Classes
Main Page  | IndexNamespace List  |  Alphabetical List  |  Class List  |  File List


Xfc::G::ThreadPool Class Reference

A GThreadPool C++ wrapper interface. More...

#include <xfc/glib/threadpool.hh>

Inheritance diagram for Xfc::G::ThreadPool:

Xfc::Object Xfc::Trackable List of all members.

Public Types

Public Member Functions

Accessors
Methods

Static Public Member Functions

Accessors
Methods

Detailed Description

A GThreadPool C++ wrapper interface.

Sometimes you wish to asynchronously fork out the execution of work and continue working in your own thread. If that will happen often, the overhead of starting and destroying a thread each time might be to high. In such cases reusing already started threads seems like a good idea. And it indeed is, but implementing this can be tedious and error-prone.

ThreadPools are provided for your convenience. An added advantage is, that the threads can be shared between the different subsystems of your program, when they are using GLib.

There is only one constructor to create a thread pool and the pool is freed automatically when it goes out of scope. A free() method is provided for use either when you want to free the thread pool before it goes out of scope, or change the default values for immediate and wait that get passed to g_thread_pool_free().

If you want to execute a certain task within a thread pool, you call push(). To get the current number of running threads you call get_num_threads(). To get the number of still unprocessed tasks you call unprocessed(). To control the maximal number of threads for a thread pool, you use get_max_threads() and set_max_threads().

Finally you can control the number of unused threads, that are kept alive by GLib for future use. The current number can be fetched with get_num_unused_threads(). The maximal number can be controlled by get_max_unused_threads() and set_max_unused_threads(). All currently unused threads can be stopped by calling stop_unused_threads().


Member Typedef Documentation

typedef sigc::slot<void> Xfc::G::ThreadPool::TaskSlot

Signature of the callback slot to be executed in the threads of the thread pool.

Example: Method signature for a TaskSlot.

< void method();
<


Constructor & Destructor Documentation

Xfc::G::ThreadPool::ThreadPool ( int  max_threads = -1,
bool  exclusive = false,
G::Error error = 0 
)

Construct a new thread pool.

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.
error The return location for a G::Error.
Whenever you call 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 exclusively 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. 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.

The error can be null to ignore errors, or non-null to report errors. An error can only occur when exclusive is set to true and not all max_threads threads could be created.

Xfc::G::ThreadPool::~ThreadPool (  ) 

Destructor.

Frees the thread pool but not before the last task currently being processed is finished running. No new tasks are processed.

Xfc::G::ThreadPool::ThreadPool ( int  max_threads = -1,
bool  exclusive = false,
G::Error error = 0 
)

Construct a new thread pool.

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.
error The return location for a G::Error.
Whenever you call 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 exclusively 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. 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.

The error can be null to ignore errors, or non-null to report errors. An error can only occur when exclusive is set to true and not all max_threads threads could be created.

Xfc::G::ThreadPool::~ThreadPool (  ) 

Destructor.

Frees the thread pool but not before the last task currently being processed is finished running. No new tasks are processed.


Member Function Documentation

int Xfc::G::ThreadPool::get_max_threads (  )  const

Gets the maximal number of threads for the pool.

Returns:
the maximal number of threads.

unsigned int Xfc::G::ThreadPool::get_num_threads (  )  const

Gets the number of threads currently running in the pool.

Returns:
The number of threads currently running.

unsigned int Xfc::G::ThreadPool::unprocessed (  )  const

Gets the number of tasks still unprocessed in the pool.

Returns:
The number of unprocessed tasks.

static unsigned int Xfc::G::ThreadPool::get_num_unused_threads (  )  [static]

Gets the number of currently unused threads.

Returns:
The number of currently unused threads.

static int Xfc::G::ThreadPool::get_max_unused_threads (  )  [static]

Gets the maximal allowed number of unused threads.

Returns:
The maximal number of unused threads.

void Xfc::G::ThreadPool::push ( const TaskSlot slot,
G::Error error = 0 
)

Inserts data into the list of tasks to be executed by the pool.

Parameters:
slot A new task to execute in the thread pool.
error The return location for a G::Error.
When the number of currently running threads is lower than the maximal allowed number of threads, a new thread is started (or reused) with the properties given to the constructor. Otherwise task stays in the queue until a thread in this pool finishes its previous task and executes task. Do not unref() the task slot when your finished. The pool will do this automatically when it gets destroyed.

The error can be null to ignore errors, or non-null to report errors. An error can only occur when a new thread couldn't be created. In that case task is simply appended to the queue of work to do.

void Xfc::G::ThreadPool::set_max_threads ( int  max_threads,
G::Error error = 0 
)

Sets the maximal allowed number of threads for pool.

Parameters:
max_threads The new maximal number of threads for the pool.
error The return location for a G::Error.
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 calling a TaskSlot. Instead the maximal number of threads only has effect for the allocation of new threads in push(). A new thread is allocated, whenever the number of currently running threads in pool is smaller than the maximal number.

The error can be null to ignore errors, or non-null to report errors. An error can only occur when a new thread couldn't be created. In that case data is simply appended to the queue of work to do.

void Xfc::G::ThreadPool::free ( bool  immediate = false,
bool  wait = true 
)

Frees all resources allocated for the pool.

Parameters:
immediate Should pool shut down immediately (default is false)?
wait Should the pool wait for all tasks to finish (default is true)?
If immediate is true, no new task is processed for the pool. Otherwise the pool is not freed before the last task is processed. Note however, that no thread of the pool is interrupted, while processing a task. Instead at least all still running threads can finish their tasks before the pool is freed.

If wait is true, the thread pool does not return before all tasks to be processed (dependent on immediate, whether all or only the currently running) are ready. Otherwise the thread pool returns immediately.

Usually you wont need to call this method because the pool is automatically freed by the destructor when the pool object goes out of scope. The default values the destructor uses to free the pool are 'immediate == false' and 'wait == true'. The only reasons to call free() is either when you want to use different values for immediate and wait, or when you want to free a pool before the pool object goes out of scope. free() is a wrapper around the C function call g_thread_pool_free() and doesn't unref() the pool object. If you created the pool dynamically you still must call unref(). However, as with all ReferencedObjects, if you created the pool on the stack you don't need to call unref(). Note after calling this method the pool must not be used anymore.

static void Xfc::G::ThreadPool::set_max_unused_threads ( int  max_threads  )  [static]

Sets the maximal number of unused threads to max_threads.

Parameters:
max_threads The maximal number of unused threads.
If max_threads is -1, no limit is imposed on the number of unused threads.

static void Xfc::G::ThreadPool::stop_unused_threads (  )  [static]

Stops all currently unused threads.

This does not change the maximal number of unused threads. This method can be used to regularly stop all unused threads.


The documentation for this class was generated from the following file: Xfce Foundation Classes
Copyright © 2004-2005 The XFC Development Team XFC 4.3