简体   繁体   English

C ++动态初始化 - 跨翻译单元

[英]C++ Dynamic initialization - across translation units

The C++98 language standard states: (My emphasis) C ++ 98语言标准规定:(我的重点)


3.6.2 Initialization of nonlocal objects 3.6.2非局部对象的初始化

£1 [...] Zeroinitialization and initialization with a constant expression are collectively called static initialization; £1 [...] Zeroinitialization和使用常量表达式初始化统称为静态初始化; all other initialization is dynamic initialization . 所有其他初始化是动态初始化 [...] [...]

£3 [...] It is implementationdefined whether or not the dynamic initialization (8.5, 9.4, 12.1, 12.6.1) of an object of namespace scope is done before the first statement of main. £3 [...]无论命名空间作用域对象的动态初始化(8.5,9.4,12.1,12.6.1)是否在main的第一个语句之前完成,都是实现定义的。 If the initialization is deferred to some point in time after the first statement of main, it shall occur before the first use of any function or object defined in the same translation unit as the object to be initialized. 如果初始化延迟到main的第一个语句之后的某个时间点, 则它应该在第一次使用与要初始化的对象相同的转换单元中定义的任何函数或对象之前发生。 [...] [...]


In my office, we have got two interpretations of the boldface passage... 在我的办公室里,我们对粗体通道有两种解释......

My question is: There is a class has a whole bunch of static methods and dynamically initialized static data members. 我的问题是:有一个类有一大堆静态方法并动态初始化静态数据成员。 Can it (or can't it) happen that static methods in this class are called from another translation unit , before dynamic initialization has been completed? 在动态初始化完成之前,是否可以(或不能)在另一个转换单元调用此类中的静态方法?

Thanks! 谢谢!

[Edit:] [编辑:]

Perhaps this boils down to the reading of "it shall occur" as: 也许这可以归结为“它应该发生”的读数:

  1. Shall have begun 应该已经开始了
  2. Shall have been completed 应该已经完成​​了

Can it (or can't it) happen that static methods in this class are called from another translation unit, before dynamic initialization has been completed? 在动态初始化完成之前,是否可以(或不能)在另一个转换单元中调用此类中的静态方法?

The bolded passage is pretty clear, isn't it? 粗体通道很清楚,不是吗? Initialization of such data is guaranteed to happen before first use of any function or class defined in its translation unit. 首次使用其翻译单元中定义的任何函数或类之前,保证初始化此类数据。 It doesn't matter where a function is called from . 这不要紧,一个函数被调用。 The guarantee is that initialization happens before the first use of any functions in the translation unit. 担保是初始化转换单元第一次使用任何功能之前发生。 Of course they may be called from another translation unit, but that doesn't make any difference. 当然,可以另一个翻译单元调用它们,但这没有任何区别。 Before a function defined in this translation unit is entered, initialization must have been performed. 在输入转换单元中定义的函数之前,必须先执行初始化。

In other words, you're safe. 换句话说,你是安全的。

... ...

assuming single-threaded execution, that is. 假设是单线程执行,即。 C++98 provides no guarantees in a multithreaded environment, so for a threaded application, the above guarantee typically just means that initialization will be performed by the first thread to use a function or class from this translation unit. C ++ 98在多线程环境中不提供任何保证,因此对于线程应用程序,上述保证通常只意味着第一个线程将执行初始化以使用此转换单元中的函数或类。 And then you have a race condition while this initialization is being performed, where other threads might hit the partially-initialized data. 然后你有一个竞争状态,同时正在执行这个初始化,其他线程可能会打到部分初始化数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM