上一篇blog里面列的关键字中的nifty counters又叫做schwarz counters,发现其他关键字用baidu search以下都有结果,但是nifty counter没有。找到了那本c++ faqs。写出关于nifty counters的使用方法

This is the most restrictive of all scenarios since it means that the construction must occur before the object is first used, and it must be destructed after its last use. The solution is called the nifty counter technique. What happens is that a static counter is created (the nifty counter) along with a static object in each source file whose constructor increments this nifty counter and whose destructor decrements the nifty counter. When the nifty counter is incremented from zero, the static object is initialized, and when the nifty counter is decremented to zero, the static object is destructed.

The following code shows how to apply this technique to the example from the previous FAQ. The static data member is back, but this time it is a static pointer. Nested class Fred::Init has a static counter (the nifty counter) called count_, which is incremented by Fred::Init's constructor and decremented by Fred::Init's destructor. The Fred::wilma_ object is created when the nifty counter is incremented from zero and is destructed when the counter is decremented back to zero. Class Wilma is not shown since it is unchanged from the example in the previous FAQ.

Here is the header file Fred.hpp.

class Fred {
public:
Fred() throw();
protected:
class Init;                                        <-- 1
friend Init;                                       <-- 2
static Wilma* wilma_;                              <-- 3
};
class Fred::Init {
public:
Init()  throw() { if (count_++ == 0) wilma_ = new Wilma(); }
~Init() throw() { if (--count_ == 0) delete wilma_; }
private:
static unsigned count_;
};
static Fred::Init fredInit;                          <-- 4
inline Fred::Fred() throw()
{
cout << "Fred ctor/n";
wilma_->f();                                       <-- 5
}

(1) This declares nested class Fred::Init

(2) So Fred::Init can access Fred::wilma_

(3) This is now a static pointer

(4) The key: a static Fred::Init object defined in the header file

(5) This is now safe

Here is the source file "Fred.cpp":

#include "Fred.hpp"
unsigned Fred::Init::count_ = 0;
Wilma* Fred::wilma_ = NULL;

Every source file that includes header file Fred.hpp ends up with its own static Fred::Init object called fredInit. Since this static object appears very early in the source file, it is initialized before most other static objects in the source file (in particular, it is guaranteed to be initialized before any static object in the source file could call Fred::Fred(), since the call to any member function of class Fred can occur only after the header of class Fred has been #included).

Of all the source files that include header file Fred.hpp, one of them, say foo.cpp, is initialized first. During the static initialization of foo.cpp, the nifty counter Fred::Init::count_ is incremented from zero, and the Fred::wilma_ object is created. Since the Fred::wilma_ object is initialized before any calls to any member functions of class Fred can be made, it is guaranteed to be constructed before it is used.

The static deinitialization situation is similar but opposite. Of all the source files that include Fred.hpp, one of them, say foo.cpp, is the last one to be deinitialized. Since deinitialization occurs in bottom to top order, the static Fred::Init object in file foo.cpp is one of the last things that is destructed (certainly it is destructed after any static object could call any member function of class Fred). Therefore the Fred::wilma_ object is destructed just after the last static object could possibly use it: it will not be used after it has been destructed.

Unfortunately the nifty counter technique also has problems. Although it never allows an object to be used either before construction or after destruction, it does force a small amount of static initialization code into every source file that includes header file Fred.hpp. This means that a large percentage of the application needs to be paged into memory during startup, which can significantly degrade startup performance, especially if there are a lot of source files that include headers that use the nifty counter technique.

关于nifty counters相关推荐

  1. Curator counters

    2019独角兽企业重金招聘Python工程师标准>>> 这个比较好理解,分布式数字,类似AtomicInteger系列,Curator有2个实现: 第一个: package cura ...

  2. java nifty_jme3与nifty gui1.3结合,修改使其支持中文的输入与显示

    1.nifty gui1.3中修改代码,使其支持中文的输入: com.jme3.input.awt.AwtKeyInput类,添加toAWTCode(int key)方法,将jme3中的keyCode ...

  3. 1.13.、1.14.Flink 支持的DataType和序列化、Flink Broadcast Accumulators Counters Distributed Cache

    1.13.Flink 支持的DataType和序列化 1.13.1.Flink支持的DataType 1.13.2.Flink的序列化 1.14.Flink Broadcast & Accum ...

  4. [CSS] Use CSS Counters to Create Pure CSS Dynamic Lists

    CSS counters let you create dynamic lists without JavaScript. In this lesson, we will create a multi ...

  5. dotnet中的counters说明(三)

    本篇分别说明一下System.Net下的Http计数器,NameResolution计数器,Security计数器和Sockets计数器. 同时,下面指标各项()里的项目是--counters 参数[ ...

  6. 编写高性能 .NET 代码 第一章:工具介绍 -- Performance Counters(性能计数器)

    Performance Counters(性能计数器) 性能计数器是监视应用程序和系统性能的最简单的方法之一.它有几十个类别数百个计数器在,包括一些.net特有的计数器.要访问这些可以通过系统自带的 ...

  7. nifty ui_Nifty JUnit:在方法和类级别上使用规则

    nifty ui 如Nifty JUnit:使用临时文件一文中所示 ,可以在JUnit测试中使用@Rule ,这是方法级别的规则. 在此示例中,我想显示@ClassRule用于类级别规则的变体. 方法 ...

  8. Nifty JUnit:在方法和类级别上使用规则

    如Nifty JUnit:使用临时文件一文中所示 ,可以在JUnit测试中使用@Rule ,这是方法级别的规则. 在此示例中,我想显示@ClassRule用于类级别规则的变体. 方法规则 @Rule在 ...

  9. Nifty File Lists for Mac(文件列表创建工具)

    Nifty File Lists for Mac是一款运行在Mac平台上的文件列表创建工具,Nifty File Lists mac版能够帮助用户保存文件列表在MS Excel.数字和页面友好的CSV ...

  10. counters.dat_使用sys.dm_os_performance_counters对SQL Server问题进行故障排除

    counters.dat sys.dm_os_performance_counters (sys.dm_os_performance_counters ) sys.dm_os_performance_ ...

最新文章

  1. [力扣] 501. 二叉搜索树中的众数
  2. 跟我一起考PMP--项目质量管理
  3. python3语法错误-【Python3之异常处理】
  4. [转]VirtualBox 复制VDI 并能创建新的虚拟机
  5. 2012传统行业转型年:整合拓展互联网发展渠道
  6. P4146 序列终结者 平衡树 + lazy维护
  7. HDU2059(DP)
  8. 【SPPS学习一】 SPSS-26软件下载与安装步骤详解
  9. android版 暴风影音,Android版暴风影音 掌上的3D影院
  10. 抖音seo,抖音搜索排名系统,抖音seo源码开发。
  11. java实现路由器重启_java 重启路由器
  12. 在腾讯待了 9 年还离了职
  13. sd卡无法读取怎么办?内存卡数据恢复,4个好用方法
  14. php 判断某一天是周几,php如何判断一个日期是周几
  15. 利用QT制作串口助手
  16. 卸载wps后office图标无法显示(亲测有效)
  17. 洛谷 P5594 【XR-4】模拟赛 记录
  18. 云运维 linux centos7.2 LAMP环境搭建 一键全解析
  19. 一文带你了解web前端是如何制作表白网站(HTML+CSS+JS)
  20. 基于bibtex的文献管理(详细)

热门文章

  1. HiC-Pro | HiC数据处理工具
  2. 测试人员用的专业截图软件,8款免费好用的截图软件工具
  3. 力扣539题 最小时间差
  4. 经济基础知识(中级)【4】
  5. 谷粒商城学习笔记(更新中)
  6. 【实战记录分析】目录导航
  7. FAQ01【Hadoop】:Could not locate executable null\bin\winutils.exe in the Hadoop binaries.
  8. php Y2K38 漏洞解决方法
  9. php warning: file_get_contents,解决PHP Warning: file_get_contents failed to open stream
  10. Context是什么