Xfce Foundation Classes
Main Page  | IndexNamespace List  |  Alphabetical List  |  Class List  |  File List


Xfc::G::StaticMutex Struct Reference

A GStaticMutex C++ wrapper interface. More...

#include <xfc/glib/mutex.hh>

List of all members.

Public Member Functions

Constructors
Accessors
Methods

Detailed Description

A GStaticMutex C++ wrapper interface.

A StaticMutex works like a Mutex, but it has one significant advantage. It doesn't need to be created at run-time like a Mutex, but can be defined at compile-time.

Here is a shorter, easier and safer version of the G::Mutex give_me_next_number() example:

Example: Using StaticMutex to simplify thread-safe programming.

    int give_me_next_number()
    {
        static int current_number = 0;
        G::StaticMutex mutex = XFC_STATIC_MUTEX_INIT;
        mutex.lock();
        int result = current_number = calc_next_number(current_number);
        mutex.unlock();
        return result;
    }

Sometimes you would like to dynamically create a mutex. If you don't want to require prior calling to G::Thread::init(), because your code should also be usable in non-threaded programs, you are not able to use a G::Mutex, as that requires a prior call to G::Thread::init(). In theses cases you can also use a StaticMutex.

A StaticMutex can be statically initialized with the value XFC_STATIC_MUTEX_INIT, and then it can directly be used, which is much easier, than having to explicitly allocate the mutex before use.


Member Function Documentation

void Xfc::G::StaticMutex::lock  ) 
 

Locks the mutex.

If mutex is already locked by another thread, the current thread will block until the mutex is unlocked by the other thread. This method can also be used if G::Thread::init() has not yet been called, in which case it will do nothing.

Note Mutex is neither guaranteed to be recursive nor to be non-recursive, that is, a thread could deadlock while calling lock(), if it already has locked the mutex. Use G::StaticRecMutex, if you need a recursive mutex.

bool Xfc::G::StaticMutex::trylock  ) 
 

Tries to lock the mutex.

Returns:
true if mutex could be locked.
If the mutex is already locked by another thread, it immediately returns false. Otherwise it locks mutex and returns true. This method can also be used if G::Thread::init() has not yet been called, in which case it will immediately return true.

Note Mutex is neither guaranteed to be recursive nor to be non-recursive, that is, the return value of trylock() could be both false or true, if the current thread already has locked the mutex. Use G::StaticRecMutex, if you need a recursive mutex.

void Xfc::G::StaticMutex::unlock  ) 
 

Unlocks the mutex.

If another thread is blocked in a lock() call for the mutex, it will be woken and can lock the mutex itself. This method can also be used if G::Thread::init() has not yet been called, in which case it will do nothing.


The documentation for this struct was generated from the following file: Xfce Foundation Classes
Copyright © 2004-2005 The XFC Development Team XFC 4.3