PageRenderTime 12ms CodeModel.GetById 2ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

Specman e | 202 lines | 7 code | 61 blank | 134 comment | 0 complexity | 51576e01831cdfb703f23dadb11fe118 MD5 | raw file
  2	copyright: "(C) 2005 Paolo Redaelli "
  3	license: "LGPL v2 or later"
  4	date: "$Date:$"
  5	revision: "$REvision:$"
  8-- Prev 	Up 	Home 	GLib Reference Manual 	Next
  9-- Top  |  Description
 10-- Thread Pools
 12-- Thread Pools %G รข€”%@ pools of threads to execute work concurrently.
 14-- Synopsis
 16-- #include <glib.h>
 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);
 46-- Description
 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.
 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.
 52-- To create a new thread pool, you use g_thread_pool_new(). It is destroyed by g_thread_pool_free().
 54-- If you want to execute a certain task within a thread pool, you call g_thread_pool_push().
 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().
 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
 62-- typedef struct {
 63--   GFunc func;
 64--   gpointer user_data;
 65--   gboolean exclusive;
 66-- } GThreadPool;
 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 ()
 74-- GThreadPool* g_thread_pool_new              (GFunc func,
 75--                                              gpointer user_data,
 76--                                              gint max_threads,
 77--                                              gboolean exclusive,
 78--                                              GError **error);
 80-- This function creates a new thread pool.
 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.
 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.
 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.
 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 ()
 96-- void        g_thread_pool_push              (GThreadPool *pool,
 97--                                              gpointer data,
 98--                                              GError **error);
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.
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.
104-- pool : 	a GThreadPool
105-- data : 	a new task for pool
106-- error : 	return location for error
107-- g_thread_pool_set_max_threads ()
109-- void        g_thread_pool_set_max_threads   (GThreadPool *pool,
110--                                              gint max_threads,
111--                                              GError **error);
113-- Sets the maximal allowed number of threads for pool. A value of -1 means, that the maximal number of threads is unlimited.
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.
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.
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.
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 ()
126-- gint        g_thread_pool_get_max_threads   (GThreadPool *pool);
128-- Returns the maximal number of threads for pool.
130-- pool : 	a GThreadPool
131-- Returns : 	the maximal number of threads
132-- g_thread_pool_get_num_threads ()
134-- guint       g_thread_pool_get_num_threads   (GThreadPool *pool);
136-- Returns the number of threads currently running in pool.
138-- pool : 	a GThreadPool
139-- Returns : 	the number of threads currently running
140-- g_thread_pool_unprocessed ()
142-- guint       g_thread_pool_unprocessed       (GThreadPool *pool);
144-- Returns the number of tasks still unprocessed in pool.
146-- pool : 	a GThreadPool
147-- Returns : 	the number of unprocessed tasks
148-- g_thread_pool_free ()
150-- void        g_thread_pool_free              (GThreadPool *pool,
151--                                              gboolean immediate,
152--                                              gboolean wait);
154-- Frees all resources allocated for pool.
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.
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.
160-- After calling this function pool must not be used anymore.
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 ()
167-- void        g_thread_pool_set_max_unused_threads
168--                                             (gint max_threads);
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.
172-- max_threads : 	maximal number of unused threads
173-- g_thread_pool_get_max_unused_threads ()
175-- gint        g_thread_pool_get_max_unused_threads
176--                                             (void);
178-- Returns the maximal allowed number of unused threads.
180-- Returns : 	the maximal number of unused threads
181-- g_thread_pool_get_num_unused_threads ()
183-- guint       g_thread_pool_get_num_unused_threads
184--                                             (void);
186-- Returns the number of currently unused threads.
188-- Returns : 	the number of currently unused threads
189-- g_thread_pool_stop_unused_threads ()
191-- void        g_thread_pool_stop_unused_threads
192--                                             (void);
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().
196-- See Also
198-- GThread 	
200-- GLib thread system.