ThreadPool


Object Hierarchy:

GLib.ThreadPool GLib.ThreadPool GLib.ThreadPool

Description:

[ Compact ]
[ CCode ( free_function = "g_thread_pool_free" ) ]
public class ThreadPool<T>

The `GThreadPool` struct represents a thread pool.

A thread pool is useful when 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 too 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.

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.

To create a new thread pool, you use [func@GLib.ThreadPool.new]. It is destroyed by [method@GLib.ThreadPool.free].

If you want to execute a certain task within a thread pool, use [method@GLib.ThreadPool.push].

To get the current number of running threads you call [method@GLib.ThreadPool.get_num_threads]. To get the number of still unprocessed tasks you call [method@GLib.ThreadPool.unprocessed]. To control the maximum number of threads for a thread pool, you use [ method@GLib.ThreadPool.get_max_threads]. and [method@GLib.ThreadPool.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 [ func@GLib.ThreadPool.get_num_unused_threads]. The maximum number can be controlled by [func@GLib.ThreadPool.get_max_unused_threads] and [ func@GLib.ThreadPool.set_max_unused_threads]. All currently unused threads can be stopped by calling [func@GLib.ThreadPool.stop_unused_threads].

Example: Thread pools:

class Worker {
public string thread_name { private set; get; }
public int x_times { private set; get; }
public int priority { private set; get; }

public Worker (string name, int x, int priority) {
this.priority = priority;
this.thread_name = name;
this.x_times = x;
}

public void run () {
for (int i = 0; i < this.x_times ; i++) {
print ("%s: %d/%d\n", this.thread_name, i + 1, this.x_times);
Thread.usleep (1000000); // wait a second
}
}
}


public static int main (string[] args) {
try {
ThreadPool<Worker> pool = new ThreadPool<Worker>.with_owned_data ((worker) => {
// Call worker.run () on thread-start
worker.run ();
}, 3, false);

// Define a priority (otpional)
pool.set_sort_function ((worker1, worker2) => {
// A simple priority-compare, qsort-style
return (worker1.priority < worker2.priority)? -1 : (int) (worker1.priority > worker2.priority);
});

// Assign some tasks:
pool.add (new Worker ("Thread 1", 5, 4));
pool.add (new Worker ("Thread 2", 10, 3));
pool.add (new Worker ("Thread 4", 5, 2));
pool.add (new Worker ("Thread 5", 5, 1));

uint waiting = pool.unprocessed (); // unfinished workers = 4
uint allowed = pool.get_max_threads (); // max running threads = 3
uint running = pool.get_num_threads (); // running threads = 3
print ("%u/%u threads are running, %u outstanding.\n", running, allowed, waiting);
} catch (ThreadError e) {
print ("ThreadError: %s\n", e.message);
}

return 0;
}

valac --pkg glib-2.0 GLib.ThreadPool.vala


Namespace: GLib
Package: glib-2.0

Content:

Static methods:

Creation methods:

Methods: