【问题标题】:How to get static constructor like initialization in GObject?如何在 GObject 中获得像初始化一样的静态构造函数?
【发布时间】:2013-10-23 09:07:23
【问题描述】:

我在 C 中使用 GLib/GObject,并且在某些情况下,我想要存在静态构造函数之类的东西,即存在于 C# 和 Java 中。

静态构造函数只会在第一次创建对象时运行一次。 C# 或 Java 中的静态构造函数的一个很好的特性是它们是线程安全的。即使多个线程同时创建该类的对象,也只有一个线程会执行静态构造函数,而其他线程(常规、非静态)构造函数会阻塞,直到该线程完成。

这使静态构造函数成为线程安全的静态字段初始化(在 C/Gobject 中只是全局变量)的理想场所。

如果您想不出需要这种功能的情况,请勾选这个可以通过静态构造函数轻松解决的问题:How to cleanly instantiate a global mutex that is shared by threads in one of the threads itself

【问题讨论】:

    标签: multithreading constructor static glib gobject


    【解决方案1】:

    把它放在class_init函数中。如果您不确定如何执行此操作,这里有一个简单的示例:

    #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 它在'g_type_register_static'中执行,即。第一次请求“Foo”的类型。可以看到,这部分函数是线程安全的。
    猜你喜欢
    • 2011-02-15
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 2014-08-25
    • 1970-01-01
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多