S60 Open C
Introduction to Glib

Introduction to Glib

Table of Contents

Using GThreads

 


Using GThreads

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 we create NTHREADS threads. We wait in the main thread for all the threads to start before proceeding with some other work. Finally we 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;
}

Give feedback of this section


©Nokia 2007

Back to top


This material, including documentation and any related computer programs, is protected by copyright controlled by Nokia. All rights are reserved. Copying, including reproducing, storing, adapting or translating, any or all of this material requires the prior written consent of Nokia. This material also contains confidential information, which may not be disclosed to others without the prior written consent of Nokia.

Nokia is a registered trademark of Nokia Corporation. S60 and logo is a trademark of Nokia Corporation. Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. Other company and product names mentioned herein may be trademarks or tradenames of their respective owners.