pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

查找C++靜態初始化順序問題

Finding C++ static initialization order problems(查找C++靜態初始化順序問題)
本文介紹了查找C++靜態初始化順序問題的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我們在 靜態初始化順序方面遇到了一些問題慘敗,我正在尋找方法來梳理大量代碼以查找可能出現的情況.關于如何有效地做到這一點有什么建議嗎?

We've run into some problems with the static initialization order fiasco, and I'm looking for ways to comb through a whole lot of code to find possible occurrences. Any suggestions on how to do this efficiently?

關于如何解決靜態初始化順序問題,我得到了一些很好的答案,但這并不是我真正的問題.我想知道如何查找受此問題影響的對象.到目前為止,埃文的回答似乎是最好的;我不認為我們可以使用 valgrind,但我們可能有可以執行類似功能的內存分析工具.這只會在給定構建的初始化順序錯誤的情況下捕獲問題,并且順序可以隨著每個構建而改變.也許有一個靜態分析工具可以解決這個問題.我們的平臺是運行在 AIX 上的 IBM XLC/C++ 編譯器.

I'm getting some good answers on how to SOLVE the static initialization order problem, but that's not really my question. I'd like to know how to FIND objects that are subject to this problem. Evan's answer seems to be the best so far in this regard; I don't think we can use valgrind, but we may have memory analysis tools that could perform a similar function. That would catch problems only where the initialization order is wrong for a given build, and the order can change with each build. Perhaps there's a static analysis tool that would catch this. Our platform is IBM XLC/C++ compiler running on AIX.

推薦答案

初始化的求解順序:

首先,這只是一個臨時的解決方法,因為您有想要擺脫的全局變量,但還沒有時間(您最終會擺脫它們,不是嗎?:-)

Solving order of initialization:

First off, this is just a temporary work-around because you have global variables that you are trying to get rid of but just have not had time yet (you are going to get rid of them eventually aren't you? :-)

class A
{
    public:
        // Get the global instance abc
        static A& getInstance_abc()  // return a reference
        {
            static A instance_abc;
            return instance_abc;
        }
};

這將保證它在第一次使用時被初始化并在應用程序終止時被銷毀.

This will guarantee that it is initialised on first use and destroyed when the application terminates.

C++11 確實保證這是線程安全的:

C++11 does guarantee that this is thread-safe:

§6.7 [stmt.dcl] p4
如果控件在變量初始化的同時進入聲明,則并發執行將等待初始化完成.

§6.7 [stmt.dcl] p4
If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization.

但是,C++03官方保證靜態函數對象的構造是線程安全的.所以從技術上講,getInstance_XXX() 方法必須用臨界區保護.從好的方面來說,gcc 有一個顯式補丁作為編譯器的一部分,保證即使存在線程,每個靜態函數對象也只會被初始化一次.

However, C++03 does not officially guarantee that the construction of static function objects is thread safe. So technically the getInstance_XXX() method must be guarded with a critical section. On the bright side, gcc has an explicit patch as part of the compiler that guarantees that each static function object will only be initialized once even in the presence of threads.

請注意:不要使用雙重檢查鎖定模式a> 盡量避免鎖定的成本.這在 C++03 中不起作用.

Please note: Do not use the double checked locking pattern to try and avoid the cost of the locking. This will not work in C++03.

在創建上,沒有問題,因為我們保證先創建后才能使用.

On creation, there are no problems because we guarantee that it is created before it can be used.

在對象被銷毀后訪問該對象存在潛在問題.只有當您從另一個全局變量的析構函數訪問對象時才會發生這種情況(全局,我指的是任何非局部靜態變量).

There is a potential problem of accessing the object after it has been destroyed. This only happens if you access the object from the destructor of another global variable (by global, I am referring to any non-local static variable).

解決辦法是確保你強制銷毀順序.
請記住,銷毀順序與構造順序正好相反.所以如果你在你的析構函數中訪問對象,你必須保證對象沒有被銷毀.為此,您必須保證在構造調用對象之前完全構造對象.

The solution is to make sure that you force the order of destruction.
Remember the order of destruction is the exact inverse of the order of construction. So if you access the object in your destructor, you must guarantee that the object has not been destroyed. To do this, you must just guarantee that the object is fully constructed before the calling object is constructed.

class B
{
    public:
        static B& getInstance_Bglob;
        {
            static B instance_Bglob;
            return instance_Bglob;;
        }

        ~B()
        {
             A::getInstance_abc().doSomthing();
             // The object abc is accessed from the destructor.
             // Potential problem.
             // You must guarantee that abc is destroyed after this object.
             // To guarantee this you must make sure it is constructed first.
             // To do this just access the object from the constructor.
        }

        B()
        {
            A::getInstance_abc();
            // abc is now fully constructed.
            // This means it was constructed before this object.
            // This means it will be destroyed after this object.
            // This means it is safe to use from the destructor.
        }
};

這篇關于查找C++靜態初始化順序問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(將 RGB 轉換為 HSV 并將 HSV 轉換為 RGB 的算法,范圍為 0-255)
How to convert an enum type variable to a string?(如何將枚舉類型變量轉換為字符串?)
When to use inline function and when not to use it?(什么時候使用內聯函數,什么時候不使用?)
Examples of good gotos in C or C++(C 或 C++ 中好的 goto 示例)
Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);(ios_base::sync_with_stdio(false) 的意義;cin.tie(NULL);)
Is TCHAR still relevant?(TCHAR 仍然相關嗎?)
主站蜘蛛池模板: 无菌实验室规划装修设计-一体化实验室承包-北京洁净净化工程建设施工-北京航天科恩实验室装备工程技术有限公司 | 意大利Frascold/富士豪压缩机_富士豪半封闭压缩机_富士豪活塞压缩机_富士豪螺杆压缩机 | 重庆小面培训_重庆小面技术培训学习班哪家好【终身免费复学】 | 恒温水槽与水浴锅-上海熙浩实业有限公司| 手板-手板模型-手板厂-手板加工-生产厂家,[东莞创域模型] | 南京技嘉环保科技有限公司-杀菌除臭剂|污水|垃圾|厕所|橡胶厂|化工厂|铸造厂除臭剂 | 上海地磅秤|电子地上衡|防爆地磅_上海地磅秤厂家–越衡称重 | 隆众资讯-首页_大宗商品资讯_价格走势_市场行情 | 山西3A认证|太原AAA信用认证|投标AAA信用证书-山西AAA企业信用评级网 | 北京亦庄厂房出租_经开区产业园招商信息平台 | 阳光模拟试验箱_高低温试验箱_高低温冲击试验箱_快速温变试验箱|东莞市赛思检测设备有限公司 | 实体店商新零售|微赢|波后|波后合作|微赢集团 | 衢州装饰公司|装潢公司|办公楼装修|排屋装修|别墅装修-衢州佳盛装饰 | 发电机组|柴油发电机组-批发,上柴,玉柴,潍柴,康明斯柴油发电机厂家直销 | 天津蒸汽/热水锅炉-电锅炉安装维修直销厂家-天津鑫淼暖通设备有限公司 | HEYL硬度计量泵-荧光法在线溶解氧仪-净时测控技术(上海)有限公司 | 插针变压器-家用电器变压器-工业空调变压器-CD型电抗器-余姚市中驰电器有限公司 | SRRC认证|CCC认证|CTA申请_IMEI|MAC地址注册-英利检测 | 彩超机-黑白B超机-便携兽用B超机-多普勒彩超机价格「大为彩超」厂家 | J.S.Bach 圣巴赫_高端背景音乐系统_官网| 云南成考网_云南成人高考报名网 粤丰硕水性环氧地坪漆-防静电自流平厂家-环保地坪涂料代理 | 火锅加盟_四川成都火锅店加盟_中国火锅连锁品牌十强_朝天门火锅【官网】 | 工程管道/塑料管材/pvc排水管/ppr给水管/pe双壁波纹管等品牌管材批发厂家-河南洁尔康建材 | 嘉兴恒升声级计-湖南衡仪声级计-杭州爱华多功能声级计-上海邦沃仪器设备有限公司 | 污水/卧式/潜水/钻井/矿用/大型/小型/泥浆泵,价格,参数,型号,厂家 - 安平县鼎千泵业制造厂 | ge超声波测厚仪-电动涂膜机-电动划格仪-上海洪富 | 小型高低温循环试验箱-可程式高低温湿热交变试验箱-东莞市拓德环境测试设备有限公司 | 搬运设备、起重设备、吊装设备—『龙海起重成套设备』 | 台湾阳明固态继电器-奥托尼克斯光电传感器-接近开关-温控器-光纤传感器-编码器一级代理商江苏用之宜电气 | 纸张环压仪-纸张平滑度仪-杭州纸邦自动化技术有限公司 | 找培训机构_找学习课程_励普教育 | 防水试验机_防水测试设备_防水试验装置_淋雨试验箱-广州岳信试验设备有限公司 | 光谱仪_积分球_分布光度计_灯具检测生产厂家_杭州松朗光电【官网】 | 注浆压力变送器-高温熔体传感器-矿用压力传感器|ZHYQ朝辉 | 阴离子聚丙烯酰胺价格_PAM_高分子聚丙烯酰胺厂家-河南泰航净水材料有限公司 | 仿古建筑设计-仿古建筑施工-仿古建筑公司-汉匠古建筑设计院 | 碳化硅,氮化硅,冰晶石,绢云母,氟化铝,白刚玉,棕刚玉,石墨,铝粉,铁粉,金属硅粉,金属铝粉,氧化铝粉,硅微粉,蓝晶石,红柱石,莫来石,粉煤灰,三聚磷酸钠,六偏磷酸钠,硫酸镁-皓泉新材料 | 胜为光纤光缆_光纤跳线_单模尾纤_光纤收发器_ODF光纤配线架厂家直销_北京睿创胜为科技有限公司 - 北京睿创胜为科技有限公司 | 万烁建筑设计院-建筑设计公司加盟,设计院加盟分公司,市政设计加盟 | 变色龙PPT-国内原创PPT模板交易平台 - PPT贰零 - 西安聚讯网络科技有限公司 | 空气弹簧|橡胶气囊|橡胶空气弹簧-上海松夏减震器有限公司 |