Reference Manual

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

# Inti::G::StaticMutex Struct Reference

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

`#include <inti/glib/mutex.h>`

List of all members.

## Public Member Functions

Constructors

- void init ()
    Initializes a static mutex. Alternatively you can initialize it with G::INTI_STATIC_MUTEX_INIT.

Accessors

- GStaticMutex * g_static_mutex () const
    Get a pointer to the GStaticMutex object.
- operator GStaticMutex * () const
    Conversion operator; Safely converts a G::StaticMutex object into a GStaticMutex pointer.

Methods

- void lock ()
    Locks the mutex.
- bool trylock ()
    Tries to lock the mutex.
- void unlock ()
    Unlocks the mutex.

## 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 = INTI_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 INTI_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 Inti::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 Inti::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 Inti::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:

- mutex.h

Main Page - Footer