Struct
GLibStaticPrivate
Description [src]
struct GStaticPrivate {
/* No available fields */
}
A GStaticPrivate
works almost like a GPrivate
, but it has one
significant advantage. It doesn’t need to be created at run-time
like a GPrivate
, but can be defined at compile-time. This is
similar to the difference between GMutex
and GStaticMutex
.
Now look at our give_me_next_number()
example with GStaticPrivate
:
int
give_me_next_number ()
{
static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
int *current_number = g_static_private_get (¤t_number_key);
if (!current_number)
{
current_number = g_new (int, 1);
*current_number = 0;
g_static_private_set (¤t_number_key, current_number, g_free);
}
*current_number = calc_next_number (*current_number);
return *current_number;
}
Instance methods
g_static_private_init
Initializes private_key
. Alternatively you can initialize it with
G_STATIC_PRIVATE_INIT
.
g_static_private_set
Sets the pointer keyed to private_key
for the current thread and
the function notify
to be called with that pointer (NULL
or
non-NULL
), whenever the pointer is set again or whenever the
current thread ends.