C++中局部静态变量的调用问题
来源:互联网 发布:济南易途java培训骗局 编辑:程序博客网 时间:2024/06/08 11:10
2.8 Initialization Guard Variables
If a function-scope static variable or a static data member with vague linkage (i.e., a static data member of a class template) is dynamically initialized, then there is an associated guard variable which is used to guarantee that construction occurs only once. The guard variable's name is mangled based on the mangling of the guarded object name. Thus, for function-scope static variables, if multiple instances of the function body are emitted (e.g., due to inlining), each function uses the same guard variable to ensure that the function-scope static is initialized only once. Similarly, if a static data member is instantiated in multiple object files, the initialization code in each object file will use the same guard variable to ensure that the static data member is initialized only once.
The size of the guard variable is 64 bits. The first byte (i.e. the byte at the address of the full variable) shall contain the value 0 prior to initialization of the associated variable, and 1 after initialization is complete. Usage of the other bytes of the guard variable is implementation-defined.
See Section 3.3.2 for the API for references to this guard variable.
if (obj_guard.first_byte == 0) { //zi如果obj_guard为0,表示还没有被初始化 if ( __cxa_guard_acquire (&obj_guard) ) { //zi获取锁,避免多线程初始化 try {... initialize the object ...; } catch (...) { __cxa_guard_abort (&obj_guard); throw; } ... queue object destructor with __cxa_atexit() ...; __cxa_guard_release (&obj_guard); //zi释放锁 } }
上面的源代码分析可以参考:深入理解函数内静态局部变量初始化
"Returns 1 if the initialization is not yet complete......"、“Sets the first byte of the guard object to a non-zero value. This function extern "C" void __cxa_guard_release ( __int64_t *guard_object ) is called after initialization is complete.....”
(b)
“说说msvc的实现。编译器自动创建两个全局变量,一个指向静态局部变量的指针,一个布尔值,用于标记是否第一次使用。下面就很简单了,第一次进函数时创建对象,以后都直接使用该对象。”
- C++中局部静态变量的调用问题
- C/C++中全局变量,静态局部变量,局部变量的初始化问题
- C语言中定义变量的技巧(全局变量、局部变量、静态全局变量、静态局部变量)
- C语言中全局变量、局部变量、静态全局变量、静态局部变量的区别
- C语言中全局变量、局部变量、静态全局变量、静态局部变量的区别
- C语言中全局变量、局部变量、静态全局变量、静态局部变量的区别
- C语言中全局变量、局部变量、静态全局变量、静态局部变量的区别
- C语言中全局变量、局部变量、静态全局变量、静态局部变量的区别
- C语言中全局变量、局部变量、静态全局变量、静态局部变量的区别
- C语言中全局变量、局部变量、静态全局变量、静态局部变量的区别
- C语言中全局变量、局部变量、静态全局变量、静态局部变量的区别
- C语言中全局变量、局部变量、静态全局变量、静态局部变量的区别
- C语言中全局变量、局部变量、静态全局变量、静态局部变量的区别
- C语言中全局变量、局部变量、静态全局变量、静态局部变量的区别
- C/C++中局部变量,静态局部变量,全局变量,静态全局变量的异同
- C语言中全局变量、局部变量、静态全局变量、静态局部变量的区别
- C语言中全局变量、局部变量、静态全局变量、静态局部变量的区别 .
- C语言中全局变量、局部变量、静态全局变量、静态局部变量的区别
- magento connection string is empty
- JDK/bin目录下的不同exe文件的用途(appletviewer、HtmlConverter、jar、java、javac、javadoc、javap、javaw、native2ascii...)
- EJB整合JSF简单的小例子
- storm 原理简介及单机版安装指南
- HTTP客户端向其他网站请求数据
- C++中局部静态变量的调用问题
- 网易云音乐可爱的tabbar
- Mutations #Facebook Relay文档翻译#
- hdu5468(容斥原理)-2015 ACM/ICPC Asia Regional Shanghai Online
- 巧用onWindowFocusChanged(boolean hasFocus)
- 日志Human Resource Evaluation
- Storm实时计算:流操作入门编程实践
- 十道海量数据处理面试题
- 内存泄露与野指针