# Summary

• Do not use threads if at all possible. (When to use threading)
• If threads have to be used, use GTask or GThreadPool and isolate the threaded code as much as possible. (Using threading)
• Use g_thread_join() to avoid leaking thread resources if using GThread manually. (Using threading)
• Be careful about the GMainContext which code is executed in if using threads. Executing code in the wrong context can cause race conditions, or block the main loop. (Using threading)

When writing projects using GLib, the default approach should be to never use threads. Instead, make proper use of the GLib main context which, through the use of asynchronous operations, allows most blocking I/O operations to continue in the background while the main context continues to process other events. Analysis, review and debugging of threaded code becomes very hard, very quickly.

Threading should only be necessary when using an external library which has blocking functions which need to be called from GLib code. If the library provides a non-blocking alternative, or one which integrates with a poll() loop, that should be used in preference. If the blocking function really must be used, a thin wrapper should be written for it to convert it to the normal GAsyncResult style of GLib asynchronous function, running the blocking operation in a worker thread.

e.g.

int some_blocking_function (void *param1, void *param2);


should be wrapped as:

void some_blocking_function_async (void *param1, void *param2, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data);
int some_blocking_function_finish (GAsyncResult *result, GError **error);


With an implementation something like:

/* Closure for the call’s parameters. */
typedef struct {
void *param1;
void *param2;
} SomeBlockingFunctionData;

static void
some_blocking_function_data_free (SomeBlockingFunctionData *data)
{
free_param (data->param1);
free_param (data->param2);

g_slice_free (SomeBlockingFunctionData, data);
}

static void
gpointer source_object,
GCancellable *cancellable)
{
int retval;

/* Handle cancellation. */
{
return;
}

/* Run the blocking function. */
retval = some_blocking_function (data->param1, data->param2);
}

void
some_blocking_function_async (void *param1,
void *param2,
GCancellable *cancellable,
gpointer user_data)
{
SomeBlockingFunctionData *data = NULL;  /* owned */

g_return_if_fail (validate_param (param1));
g_return_if_fail (validate_param (param2));
g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));

/* Cancellation should be handled manually using mechanisms specific to
* some_blocking_function(). */

/* Set up a closure containing the call’s parameters. Copy them to avoid
* locking issues between the calling thread and the worker thread. */
data = g_slice_new (SomeBlockingFunctionData);
data->param1 = copy_param (param1);
data->param2 = copy_param (param2);

/* Run the task in a worker thread and return immediately while that continues
* in the background. When it’s done it will call @callback in the current
* thread default main context. */

}

int
some_blocking_function_finish (GAsyncResult *result,
GError **error)
{
some_blocking_function_async), -1);
g_return_val_if_fail (error == NULL || *error == NULL, -1);

}


See the GAsyncResult documentation for more details. A simple way to implement the worker thread is to use GTask and g_task_run_in_thread().

If GTask is not suitable for writing the worker thread, a more low-level approach must be used. This should be considered very carefully, as it is very easy to get threading code wrong in ways which will unpredictably cause bugs at runtime, cause deadlocks, or consume too many resources and terminate the program.

A full manual on writing threaded code is beyond the scope of this document, but here are a number of guidelines to follow which should reduce the potential for bugs in threading code. The overriding principle is to reduce the amount of code and data which can be affected by threading — e.g. reducing the number of threads, the complexity of worker thread implementation, and the amount of data shared between threads.

• Use GThreadPool instead of manually creating GThreads if possible. GThreadPool supports a work queue, limits on the number of spawned threads, and automatically joins finished threads so they are not leaked.
• If it is not possible to use a GThreadPool (which is rarely the case):
• Use message passing to transfer data between threads, rather than manual locking with mutexes. GThreadPool explicitly supports this with g_thread_pool_push().
• If mutexes must be used:
• Isolate threading code as much as possible, keeping mutexes private within classes, and tightly bound to very specific class members.
• All mutexes should be clearly commented beside their declaration, indicating which other structures or variables they protect access to. Similarly, those variables should be commented saying that they should only be accessed with that mutex held.
• Be careful about interactions between main contexts and threads. For example, g_timeout_add_seconds() adds a timeout to be executed in the global default main context, which is being run in the main thread, not necessarily the current thread. Getting this wrong can mean that work intended for a worker thread accidentally ends up being executed in the main thread anyway.

# Debugging

Debugging threading issues is tricky, both because they are hard to reproduce, and because they are hard to reason about. This is one of the big reasons for avoiding using threads in the first place.

However, if a threading issue does arise, Valgrind’s drd and helgrind tools are useful.