/src/wrappers/glib/library/core/glib_thread_pools.e
Specman e | 202 lines | 7 code | 61 blank | 134 comment | 0 complexity | 51576e01831cdfb703f23dadb11fe118 MD5 | raw file
1indexing 2 copyright: "(C) 2005 Paolo Redaelli " 3 license: "LGPL v2 or later" 4 date: "$Date:$" 5 revision: "$REvision:$" 6 7class GLIB_THREAD_POOLS 8-- Prev Up Home GLib Reference Manual Next 9-- Top | Description 10-- Thread Pools 11 12-- Thread Pools %G รข€”%@ pools of threads to execute work concurrently. 13 14-- Synopsis 15 16-- #include <glib.h> 17 18 19-- GThreadPool; 20-- GThreadPool* g_thread_pool_new (GFunc func, 21-- gpointer user_data, 22-- gint max_threads, 23-- gboolean exclusive, 24-- GError **error); 25-- void g_thread_pool_push (GThreadPool *pool, 26-- gpointer data, 27-- GError **error); 28-- void g_thread_pool_set_max_threads (GThreadPool *pool, 29-- gint max_threads, 30-- GError **error); 31-- gint g_thread_pool_get_max_threads (GThreadPool *pool); 32-- guint g_thread_pool_get_num_threads (GThreadPool *pool); 33-- guint g_thread_pool_unprocessed (GThreadPool *pool); 34-- void g_thread_pool_free (GThreadPool *pool, 35-- gboolean immediate, 36-- gboolean wait); 37-- void g_thread_pool_set_max_unused_threads 38-- (gint max_threads); 39-- gint g_thread_pool_get_max_unused_threads 40-- (void); 41-- guint g_thread_pool_get_num_unused_threads 42-- (void); 43-- void g_thread_pool_stop_unused_threads 44-- (void); 45 46-- Description 47 48-- Sometimes you wish to asyncronously 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. 49 50-- Therefore GLib provides thread pools 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. 51 52-- To create a new thread pool, you use g_thread_pool_new(). It is destroyed by g_thread_pool_free(). 53 54-- If you want to execute a certain task within a thread pool, you call g_thread_pool_push(). 55 56-- To get the current number of running threads you call g_thread_pool_get_num_threads(). To get the number of still unprocessed tasks you call g_thread_pool_unprocessed(). To control the maximal number of threads for a thread pool, you use g_thread_pool_get_max_threads() and g_thread_pool_set_max_threads(). 57 58-- 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 g_thread_pool_get_num_unused_threads(). The maximal number can be controlled by g_thread_pool_get_max_unused_threads() and g_thread_pool_set_max_unused_threads(). All currently unused threads can be stopped by calling g_thread_pool_stop_unused_threads(). 59-- Details 60-- GThreadPool 61 62-- typedef struct { 63-- GFunc func; 64-- gpointer user_data; 65-- gboolean exclusive; 66-- } GThreadPool; 67 68-- The GThreadPool struct represents a thread pool. It has six public read-only members, but the underlying struct is bigger, so you must not copy this struct. 69-- GFunc func; the function to execute in the threads of this pool 70-- gpointer user_data; the user data for the threads of this pool 71-- gboolean exclusive; are all threads exclusive to this pool 72-- g_thread_pool_new () 73 74-- GThreadPool* g_thread_pool_new (GFunc func, 75-- gpointer user_data, 76-- gint max_threads, 77-- gboolean exclusive, 78-- GError **error); 79 80-- This function creates a new thread pool. 81 82-- Whenever you call g_thread_pool_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 newly created or reused thread now executes the function func with the two arguments. The first one is the parameter to g_thread_pool_push() and the second one is user_data. 83 84-- 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 g_thread_pool_free(). 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. 85 86-- 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. 87 88-- func : a function to execute in the threads of the new thread pool 89-- user_data : user data that is handed over to func every time it is called 90-- max_threads : the maximal number of threads to execute concurrently in the new thread pool, -1 means no limit 91-- exclusive : should this thread pool be exclusive? 92-- error : return location for error 93-- Returns : the new GThreadPool 94-- g_thread_pool_push () 95 96-- void g_thread_pool_push (GThreadPool *pool, 97-- gpointer data, 98-- GError **error); 99 100-- Inserts data into the list of tasks to be executed by pool. 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 g_thread_pool_new(). Otherwise data stays in the queue until a thread in this pool finishes its previous task and processes data. 101 102-- 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. 103 104-- pool : a GThreadPool 105-- data : a new task for pool 106-- error : return location for error 107-- g_thread_pool_set_max_threads () 108 109-- void g_thread_pool_set_max_threads (GThreadPool *pool, 110-- gint max_threads, 111-- GError **error); 112 113-- Sets the maximal allowed number of threads for pool. A value of -1 means, that the maximal number of threads is unlimited. 114 115-- 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. 116 117-- A thread is never terminated while calling func, as supplied by g_thread_pool_new(). Instead the maximal number of threads only has effect for the allocation of new threads in g_thread_pool_push(). A new thread is allocated, whenever the number of currently running threads in pool is smaller than the maximal number. 118 119-- 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. 120 121-- pool : a GThreadPool 122-- max_threads : a new maximal number of threads for pool 123-- error : return location for error 124-- g_thread_pool_get_max_threads () 125 126-- gint g_thread_pool_get_max_threads (GThreadPool *pool); 127 128-- Returns the maximal number of threads for pool. 129 130-- pool : a GThreadPool 131-- Returns : the maximal number of threads 132-- g_thread_pool_get_num_threads () 133 134-- guint g_thread_pool_get_num_threads (GThreadPool *pool); 135 136-- Returns the number of threads currently running in pool. 137 138-- pool : a GThreadPool 139-- Returns : the number of threads currently running 140-- g_thread_pool_unprocessed () 141 142-- guint g_thread_pool_unprocessed (GThreadPool *pool); 143 144-- Returns the number of tasks still unprocessed in pool. 145 146-- pool : a GThreadPool 147-- Returns : the number of unprocessed tasks 148-- g_thread_pool_free () 149 150-- void g_thread_pool_free (GThreadPool *pool, 151-- gboolean immediate, 152-- gboolean wait); 153 154-- Frees all resources allocated for pool. 155 156-- If immediate is TRUE, no new task is processed for pool. Otherwise 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. 157 158-- If wait is TRUE, the functions does not return before all tasks to be processed (dependent on immediate, whether all or only the currently running) are ready. Otherwise the function returns immediately. 159 160-- After calling this function pool must not be used anymore. 161 162-- pool : a GThreadPool 163-- immediate : should pool shut down immediately? 164-- wait : should the function wait for all tasks to be finished? 165-- g_thread_pool_set_max_unused_threads () 166 167-- void g_thread_pool_set_max_unused_threads 168-- (gint max_threads); 169 170-- 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. 171 172-- max_threads : maximal number of unused threads 173-- g_thread_pool_get_max_unused_threads () 174 175-- gint g_thread_pool_get_max_unused_threads 176-- (void); 177 178-- Returns the maximal allowed number of unused threads. 179 180-- Returns : the maximal number of unused threads 181-- g_thread_pool_get_num_unused_threads () 182 183-- guint g_thread_pool_get_num_unused_threads 184-- (void); 185 186-- Returns the number of currently unused threads. 187 188-- Returns : the number of currently unused threads 189-- g_thread_pool_stop_unused_threads () 190 191-- void g_thread_pool_stop_unused_threads 192-- (void); 193 194-- 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 g_timeout_add(). 195 196-- See Also 197 198-- GThread 199 200-- GLib thread system. 201 202end