class Foo
{
public:
staticFoo& instance(void)
{
static Foo s_instance;
return s_instance;
}
}; 以下摘自ISOIEC 14882 C++11
6.7.4
The zero-initialization (8.5) of all block-scope variables with static storage duration (3.7.1) or thread storage duration (3.7.2) is performed before any other initialization takes place. Constant initialization (3.6.2) of a block-scope entity with static storage duration, if applicable, is performed before its block is first entered. An implementation is permitted to perform early initialization of other block-scope variables with static or thread storage duration under the same conditions that an implementation is permitted to statically initialize a variable with static or thread storage duration in namespace scope (3.6.2). Otherwise such a variable is initialized the first time control passes through its declaration; such a variable is considered initialized upon the completion of its initialization. If the initialization exits by throwing an exception, the initialization is not complete, so it will be tried again the next time control enters the declaration. If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization.88 If control re-enters the declaration recursively while the variable is being initialized, the behavior is undefined.

C++0x对局部静态初始化作出了线程安全的要求,singleton的写法可以回归到最原始的方式的更多相关文章

  1. C++中的局部变量、全局变量、局部静态变量、全局静态变量的区别

    局部变量(Local variables)与 全局变量: 在子程序或代码块中定义的变量称为局部变量,在程序的一开始定义的变量称为全局变量. 全局变量作用域是整个程序,局部变量作用域是定义该变量的子程序 ...

  2. 自动对象&局部静态对象

    一.关键点 对象的生命周期:程序执行过程中,该对象存在的那段时间 局部对象:形参.函数体内部定义的变量 二.自动对象 自动对象:只存在于块执行期间的对象 包括:局部变量.形参 三.局部静态对象 特点: ...

  3. c++ 反汇编 局部静态变量

    vs2017下测试 34: for (int i = 0; i < 5; i++) 0029734E C7 45 F8 00 00 00 00 mov dword ptr [ebp-8],0 0 ...

  4. Java的初始化块、静态初始化块、构造函数的执行顺序及用途探究

    Java与C++有一个不同之处在于,Java不但有构造函数,还有一个”初始化块“(Initialization Block)的概念.下面探究一下它的执行顺序与可能的用途. 执行顺序 首先定义A, B, ...

  5. Java 中的 static 使用之静态初始化块

    Java 中可以通过初始化块进行数据赋值.如: 在类的声明中,可以包含多个初始化块,当创建类的实例时,就会依次执行这些代码块.如果使用 static 修饰初始化块,就称为静态初始化块. 需要特别注意: ...

  6. java中的静态初始化块

    Java 中可以通过初始化块进行数据赋值.如: 在类的声明中,可以包含多个初始化块,当创建类的实例时,就会依次执行这些代码块.如果使用 static 修饰初始化块,就称为静态初始化块. 需要特别注意: ...

  7. static静态初始化块

    Java 中可以通过初始化块进行数据赋值.如: 在类的声明中,可以包含多个初始化块,当创建类的实例时,就会依次执行这些代码块.如果使用 static 修饰初始化块,就称为静态初始化块. 需要特别注意: ...

  8. java静态初始化代码块

    /* * 为什么Java中为什么没有静态构造函数.其实Java中不叫静态构造函数,称作静态初始化,或者静态代码块. * 可以通过这样的代码实现相同的功能: */ public class test { ...

  9. JAVA之旅(三)——数组,堆栈内存结构,静态初始化,遍历,最值,选择/冒泡排序,二维数组,面向对象思想

    JAVA之旅(三)--数组,堆栈内存结构,静态初始化,遍历,最值,选择/冒泡排序,二维数组,面向对象思想 我们继续JAVA之旅 一.数组 1.概念 数组就是同一种类型数据的集合,就是一个容器 数组的好 ...

随机推荐

  1. jquery刷新iframe页面的方法

    1,reload 方法,该方法强迫浏览器刷新当前页面. 语法:location.reload([bForceGet]) 参数: bForceGet, 可选参数, 默认为 false,从客户端缓存里取当 ...

  2. android自定义样式大全:shape,selector,layer-list,style,动画全部内容

    原文:http://keeganlee.me/post/android/20150830 以下摘取了部分内容: shape 一般用shape定义的xml文件存放在drawable目录下,若项目没有该目 ...

  3. 如何解决在GDI画图中,多次修改画笔的颜色

    首先创建个画笔对象: CPen gPen;gPen.CreatePen(PS_SOLID, 1, RGB(120,120,130));//一定灰度的画笔〈/br〉CPen* pOldPen = pDC ...

  4. boost::unordered_map 和 std::map 的效率 与 内存比较

    例子链接:http://blog.csdn.net/gamecreating/article/details/7698719 结论: unordered_map 查找效率快五倍,插入更快,节省一定内存 ...

  5. windows 安装maven 环境

    1.maven 下载地址: http://maven.apache.org/index.html 2.解压到目录并配置环境变量 M2_HOME   D:\maven\maven path       ...

  6. [Machine Learning] Probabilistic Graphical Models:二、Bayes Network Fundamentals(1、Semantics & Factorization)

    一.How to construct the dependency? 1.首字母即随机变量名称 2.I->G是更加复杂的模型,但Bayes里不考虑,因为Bayes只是无环图. 3.CPD = c ...

  7. JqGrid实现自定义查询

    $("#jqGridId").setGridParam({url:"数据查询地址"}).trigger("reloadGrid");

  8. include()、include_once()与require()、require_once()的异同点

    相同点: 首先include().include_once()与require().require_once()都是用来包含并运行指定文件的,并且包含的文件在执行时在结构上是完全一样的. 例如:inc ...

  9. smarty模板引擎原理解析

    //php 控制器文件 <?php//引入模板引擎文件include("20130304.php");$smarty =newTinySmarty();$qq_numbers ...

  10. Django 基础

    Django 的路由系统 在 django 的 URLconf 配置文件 urls.py 中根据一个 URL 对应 views 的一个函数来处理用户的请求. 1.基本的 urls 对应 urlpatt ...