如何确保以正确的顺序初始化全局变量?

时间:2015-10-06 20:23:16

标签: c++ c++14

我有全局变量,其构造函数依赖于不同翻译单元中的其他全局变量。我的理解是全局变量的初始化顺序是未指定的,因此这不安全。

我怎样才能确保我们需要的全局变量最初会在首次访问时初始化?例如,如果我在函数中创建一个静态变量并调用该函数来获取引用,那么它是否会在第一次执行函数时被初始化?

1 个答案:

答案 0 :(得分:6)

您可以使用与标准流std::cout及其朋友使用的方法相同的方法。它被称为Schwarz Counter or Nifty Counter

如果您查看GNU ios_base.h的{​​{1}}标题:

libstdc++

进入// 27.4.2.1.6 Class ios_base::Init // Used to initialize standard streams. In theory, g++ could use // -finit-priority to order this stuff correctly without going // through these machinations. class Init { friend class ios_base; public: Init(); ~Init(); private: static _Atomic_word _S_refcount; static bool _S_synced_with_stdio; }; 标题:

iostream

进入namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION extern istream cin; /// Linked to standard input extern ostream cout; /// Linked to standard output extern ostream cerr; /// Linked to standard error (unbuffered) extern ostream clog; /// Linked to standard error (buffered) // For construction of filebuffers for cout, cin, cerr, clog et. al. static ios_base::Init __ioinit; _GLIBCXX_END_NAMESPACE_VERSION } // namespace

ios_init.cc

上面将具有静态存储持续时间的全局对象 ios_base::Init::Init() { if (__gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, 1) == 0) { // Standard streams default to synced with "C" operations. _S_synced_with_stdio = true; new (&buf_cout_sync) stdio_sync_filebuf<char>(stdout); new (&buf_cin_sync) stdio_sync_filebuf<char>(stdin); new (&buf_cerr_sync) stdio_sync_filebuf<char>(stderr); // The standard streams are constructed once only and never // destroyed. new (&cout) ostream(&buf_cout_sync); new (&cin) istream(&buf_cin_sync); new (&cerr) ostream(&buf_cerr_sync); new (&clog) ostream(&buf_cerr_sync); cin.tie(&cout); cerr.setf(ios_base::unitbuf); // _GLIBCXX_RESOLVE_LIB_DEFECTS // 455. cerr::tie() and wcerr::tie() are overspecified. cerr.tie(&cout); // NB: Have to set refcount above one, so that standard // streams are not re-initialized with uses of ios_base::Init // besides <iostream> static object, ie just using <ios> with // ios_base::Init objects. __gnu_cxx::__atomic_add_dispatch(&_S_refcount, 1); } } ios_base::Init::~Init() { // Be race-detector-friendly. For more info see bits/c++config. _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_S_refcount); if (__gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, -1) == 2) { _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_S_refcount); // Catch any exceptions thrown by basic_ostream::flush() __try { // Flush standard output streams as required by 27.4.2.1.6 cout.flush(); cerr.flush(); clog.flush(); } __catch(...) { } } } 嵌入到包含__ioinit头文件的每个翻译单元(.o)中。即每个iostream都有自己的.o副本。

具有静态存储持续时间的基本类型的所有对象在静态初始化阶段(在Linux上这是elf对象的__ioinit部分)启动时初始化为零,因此.bss被赋值为0在动态初始化阶段之前。

接下来,在动态初始化期间,将调用这些_S_refcount个对象的构造函数。每个构造函数递增__ioinit,并且观察到0 _S_refcount值的__ioinit对象驻留在首先初始化的转换单元中。该对象的构造函数初始化标准流。

C ++标准库缺陷报告列表Issue 369: io stream objects and static ctors中有更多信息。

您可以使用相同的方法初始化自己的全局对象。 E.g:

_S_refcount

用法:

// DynamicInitializer.h
template<class T>
struct DynamicInitializer
{
    // These members have to be POD types to be zero-initialized at static initialization phase
    // prior to the dynamic initialization phase which invokes constructors of global objects.
    static T* instance_;
    static unsigned ref_count_;

    DynamicInitializer() {
        if(!ref_count_++)
            instance_ = new T;
    }

    ~DynamicInitializer() {
        if(!--ref_count_)
            delete instance_;
    }

    operator T&() const { return *instance_; }
    T* operator->() const { return instance_; }

    DynamicInitializer(DynamicInitializer const&) = delete;
    DynamicInitializer& operator=(DynamicInitializer const&) = delete;
};

template<class T>
unsigned DynamicInitializer<T>::ref_count_ = 0;

template<class T>
T* DynamicInitializer<T>::instance_ = 0;

现在,只要包含// MyLogger.h struct MyLogger { void log(char const*); }; // const makes static storage. DynamicInitializer<MyLogger> const my_global_logger; ,就可以保证MyLogger.h在首次使用之前进行初始化,例如my_global_logger