The function defined by the gobject parent class in the class structure must be thread safe.

Source: Internet
Author: User
In gobject, we usually place the function to be inherited from the subclass in the class structure for the inherited class to be reloaded. The general logic is that after the inherited class is overloaded, the custom logic is implemented, and then the default Implementation of the function of the parent class is called. This will cause a problem: multiple child classes may call a function default Implementation of the parent class at the same time. In this way, the implementation of these functions of the parent class must be thread safe.

 

The reason for not placing these function pointers in the instance structure of the parent class is that, when the child class is overloaded, the default Implementation of the parent class cannot be called, this will cause code redundancy and difficulty in maintenance.

 

To verify the above idea, I wrote a program. Parent class Dad, two inherited classes son and daughter. Through the test program, we can see that in a dad_print function opened by the parent class, both son and daughter overload this function. When both son and daughter call the default Implementation of the parent class dad_print, the addresses of the two function pointers are the same. Therefore, according to this test program and the gobject documentation, the memory structure and initialization logic of gobject should be as follows:

 

1. g_object_new creates a gobject. 2. For the first g_object_new, from the parent class, all the way down until the final subclass, the class structure of the class will be created. In its class structure, because the first member is the class structure of the parent class, in the memory, the class structure of the inherited class should be the class structure of the parent class, next we will inherit the class's own. 3. Each time g_object_new, the instance structure will be new. It is also the parent class's instance structure starting down, and then the subclass's instance structure. Because there may be multiple instances of a class, there is no such thing as parent_instance, only parent_class.

 

The above description should be shown as follows:

 

Test code:/files/super119/gobjclsfuncreent.zip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.