StaticMutex


Object Hierarchy:

GLib.StaticMutex GLib.StaticMutex GLib.StaticMutex

Description:

[ Version ( deprecated_since = "2.32" , replacement = "Mutex" ) ]
[ CCode ( default_value = "G_STATIC_MUTEX_INIT" , destroy_function = "g_static_mutex_free" , has_type_id = false ) ]
public struct StaticMutex

Warning: StaticMutex is deprecated since 2.32. Use Mutex.

A StaticMutex works like a Mutex.

Prior to GLib 2.32, GStaticMutex had the significant advantage that it doesn't need to be created at run-time, but can be defined at compile-time. Since 2.32, Mutex can be statically allocated as well, and GStaticMutex has been deprecated.

Here is a version of our give_me_next_number example using a GStaticMutex:

  int
give_me_next_number (void)
{
static int current_number = 0;
int ret_val;
static GStaticMutex mutex = G_STATIC_MUTEX_INIT;

g_static_mutex_lock (&mutex);
ret_val = current_number = calc_next_number (current_number);
g_static_mutex_unlock (&mutex);

return ret_val;
}

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 g_mutex_new and thus Mutex, as that requires a prior call to g_thread_init. In these cases you can also use a StaticMutex. It must be initialized with StaticMutex before using it and freed with with g_static_mutex_free when not needed anymore to free up any allocated resources.

Even though StaticMutex is not opaque, it should only be used with the following functions, as it is defined differently on different platforms.

All of the g_static_mutex_* functions apart from g_static_mutex_get_mutex can also be used even if g_thread_init has not yet been called. Then they do nothing, apart from trylock which does nothing but returning true.

All of the g_static_mutex_* functions are actually macros. Apart from taking their addresses, you can however use them as if they were functions.


Namespace: GLib
Package: glib-2.0

Content:

Creation methods:

Methods: