GLib threads provide a portable way of writing multi-threaded applications. There are:
primitives for mutexes to protect concurrent accesses to portions of the memory
primitives for conditional variables to allow synchronization between threads
primitives for thread-private data that every thread has an instance of
APIs for creating and managing threads.
To use thread-related APIs the application must link to libgthread.lib
.
The function g_thread_init()
must be called to initialize
the thread system before calling any other thread-related functions.
Note: The function
g_thread_init()
must be called only once.
The second call to it will abort the application.The code given below verifies whether the thread system is initialized.
if (!g_thread_supported ()) g_thread_init (NULL);
g_thread_init()
can be called with a non-NULL parameter;
however, the user must be sure of what is done since this will override the
default thread implementation.
The following example code explains the usage of some of the thread APIs.
In the example code, create NTHREADS
thread and wait in the
main thread for all the threads to start before proceeding with some other
work. Finally, join to all the threads so that the main thread waits for all
the other threads to terminate.
#include <glib.h> /* g_mutex_lock is a macro that uses the dll global variable * g_thread_functions_for_glib_use. To use the global variable * we must include glib_global.h. */ #include <glib_global.h> // Always include as the last include. #define NTHREADS 5 int no_threads = 0; GMutex *mutex; GCond *cond; void* thread_function(void *data) { /* . local data declaration . */ // lock on the mutex before changing the global variable no_threads g_mutex_lock(mutex); no_threads++; //do a broadcast on the conditional variable once NTHREDS have been created if(no_threads == NTHREADS) g_cond_broadcast(cond); //unlock the mutex g_mutex_unlock(mutex); /* . some code . */ return NULL; } int main () { int i; GThread *thread[NTHREADS]; /* . other local data declaration . */ if(!g_thread_supported()) g_thread_init (NULL); mutex = g_mutex_new (); cond = g_cond_new (); for(i=0; i<NTHREADS; i++) thread[i] = g_thread_create(thread_function, NULL,TRUE, NULL); g_mutex_lock (mutex); if (no_threads < NTHREADS) g_cond_wait (cond, mutex); g_mutex_unlock (mutex); /* . some code . */ for(i=0; i<NTHREADS; i++) g_thread_join(thread[i]); g_mutex_free(mutex); g_cond_free(cond); /* . some code . */ return 0; }