std::thread::hardware_concurrency 和静态初始化
std::thread::hardware_concurrency and static initialization
这个全局函数是否会遭受静态初始化失败?
template <typename TFn>
void ParallelFor(int iIni,int iFin,TFn Fn)
{
static const unsigned int NThread= std::thread::hardware_concurrency();
// ...
}
May this global function suffer from static initialization fiasco?
不,不会。你很安全...:-)
引用 C++ 标准草案(强调我的)...
.7: 4: Dynamic initialization of a block-scope variable with
static storage duration ([basic.stc.static]) or thread storage
duration ([basic.stc.thread]) is performed 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
由于您的函数是函数模板 template <typename TFn>
,对于每个单独的实例化(TFn
的替代),static const unsigned int NThread = std::thread::hardware_concurrency();
将被评估
这个全局函数是否会遭受静态初始化失败?
template <typename TFn>
void ParallelFor(int iIni,int iFin,TFn Fn)
{
static const unsigned int NThread= std::thread::hardware_concurrency();
// ...
}
May this global function suffer from static initialization fiasco?
不,不会。你很安全...:-)
引用 C++ 标准草案(强调我的)...
.7: 4: Dynamic initialization of a block-scope variable with static storage duration ([basic.stc.static]) or thread storage duration ([basic.stc.thread]) is performed 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
由于您的函数是函数模板 template <typename TFn>
,对于每个单独的实例化(TFn
的替代),static const unsigned int NThread = std::thread::hardware_concurrency();
将被评估