May this global function suffer from static initialization fiasco?
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?
No, it wouldn't. You are safe... :-)
Quoting the C++ standard draft (emphasis mine)...
$6.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
Also, see: Static local variables
Since your function is a function template template <typename TFn>
, for each separate instantiation (substitution of TFn
), static const unsigned int NThread = std::thread::hardware_concurrency();
will be evaluated
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With