把它放在類初始化函數。如果你不確定如何做到這一點,這裏是一個微不足道的例子:
#include <glib.h>
#include <glib-object.h>
#define TYPE_FOO (foo_get_type())
#define FOO(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_FOO, Foo))
#define FOO_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_FOO, FooClass))
#define IS_FOO(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_FOO))
#define IS_FOO_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_FOO))
#define FOO_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_FOO, FooClass))
typedef struct _Foo Foo;
typedef struct _FooClass FooClass;
typedef struct _FooPrivate FooPrivate;
struct _Foo {
GObject parent_instance;
FooPrivate * priv;
};
struct _FooClass {
GObjectClass parent_class;
};
static gpointer foo_parent_class = NULL;
GType foo_get_type (void) G_GNUC_CONST;
enum {
FOO_DUMMY_PROPERTY
};
Foo* foo_new (void);
Foo* foo_construct (GType object_type);
Foo* foo_construct (GType object_type) {
Foo * self = NULL;
self = (Foo*) g_object_new (object_type, NULL);
return self;
}
Foo* foo_new (void) {
return foo_construct (TYPE_FOO);
}
static void foo_class_init (FooClass * klass) {
foo_parent_class = g_type_class_peek_parent (klass);
/* static construct code goes here */
}
static void foo_instance_init (Foo * self) {
}
GType foo_get_type (void) {
static volatile gsize foo_type_id__volatile = 0;
if (g_once_init_enter (&foo_type_id__volatile)) {
static const GTypeInfo g_define_type_info = { sizeof (FooClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) foo_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (Foo), 0, (GInstanceInitFunc) foo_instance_init, NULL };
GType foo_type_id;
foo_type_id = g_type_register_static (G_TYPE_OBJECT, "Foo", &g_define_type_info, 0);
g_once_init_leave (&foo_type_id__volatile, foo_type_id);
}
return foo_type_id__volatile;
}
我想知道爲什麼這是線程安全的。什麼時候_class_init函數準確執行? – Dyna
@Dyna它在'g_type_register_static'中執行,即。第一次請求輸入「Foo」的類型。正如你所看到的,這部分功能是線程安全的。 – Ancurio