gthread.Mutex.Mutex Class Reference

List of all members.

Detailed Description

Description Threads act almost like processes, but unlike processes all threads of one process share the same memory.

This is good, as it provides easy communication between the involved threads via this shared memory, and it is bad, because strange things (so called "Heisenbugs") might happen if the program is not carefully designed. In particular, due to the concurrent nature of threads, no assumptions on the order of execution of code running in different threads can be made, unless order is explicitly forced by the programmer through synchronization primitives. The aim of the thread related functions in GLib is to provide a portable means for writing multi-threaded software. There are primitives for mutexes to protect the access to portions of memory (GMutex, GStaticMutex, G_LOCK_DEFINE, GStaticRecMutex and GStaticRWLock). There are primitives for condition variables to allow synchronization of threads (GCond). There are primitives for thread-private data - data that every thread has a private instance of (GPrivate, GStaticPrivate). Last but definitely not least there are primitives to portably create and manage threads (GThread). You must call g_thread_init() before executing any other GLib functions in a threaded GLib program. After that, GLib is completely thread safe (all global data is automatically locked), but individual data structure instances are not automatically locked for performance reasons. So, for example you must coordinate accesses to the same GHashTable from multiple threads. The two notable exceptions from this rule are GMainLoop and GAsyncQueue, which are threadsafe and needs no further application-level locking to be accessed from multiple threads.


Public Member Functions

GMutexgetMutexStruct ()
 this (GMutex *gMutex)
 Sets our main struct and passes it to the parent class.
 this ()
 Creates a new GMutex.
void lock ()
 Locks mutex.
int trylock ()
 Tries to lock mutex.
void unlock ()
 Unlocks mutex.
void free ()
 Destroys mutex.

Protected Member Functions

void * getStruct ()
 the main Gtk struct as a void*

Protected Attributes

GMutexgMutex
 the main Gtk struct


Constructor & Destructor Documentation

gthread.Mutex.Mutex.this ( GMutex gMutex  ) 

Sets our main struct and passes it to the parent class.

gthread.Mutex.Mutex.this (  ) 

Creates a new GMutex.

Note This function will abort if g_thread_init() has not been called yet. Returns: a new GMutex.


Member Function Documentation

void gthread.Mutex.Mutex.free (  ) 

Destroys mutex.

mutex: a GMutex.

GMutex* gthread.Mutex.Mutex.getMutexStruct (  ) 

void* gthread.Mutex.Mutex.getStruct (  )  [protected]

the main Gtk struct as a void*

void gthread.Mutex.Mutex.lock (  ) 

Locks mutex.

If mutex is already locked by another thread, the current thread will block until mutex is unlocked by the other thread. This function can be used even if g_thread_init() has not yet been called, and, in that case, will do nothing. Note GMutex is neither guaranteed to be recursive nor to be non-recursive, i.e. a thread could deadlock while calling g_mutex_lock(), if it already has locked mutex. Use GStaticRecMutex, if you need recursive mutexes. mutex: a GMutex.

int gthread.Mutex.Mutex.trylock (  ) 

Tries to lock mutex.

If mutex is already locked by another thread, it immediately returns FALSE. Otherwise it locks mutex and returns TRUE. This function can be used even if g_thread_init() has not yet been called, and, in that case, will immediately return TRUE. Note GMutex is neither guaranteed to be recursive nor to be non-recursive, i.e. the return value of g_mutex_trylock() could be both FALSE or TRUE, if the current thread already has locked mutex. Use GStaticRecMutex, if you need recursive mutexes. mutex: a GMutex. Returns: TRUE, if mutex could be locked.

void gthread.Mutex.Mutex.unlock (  ) 

Unlocks mutex.

If another thread is blocked in a g_mutex_lock() call for mutex, it will be woken and can lock mutex itself. This function can be used even if g_thread_init() has not yet been called, and, in that case, will do nothing. mutex: a GMutex.


Member Data Documentation

GMutex* gthread.Mutex.Mutex.gMutex [protected]

the main Gtk struct


SourceForge.net Logo DSource.org Logo digitalmars.com Logo