Thomas Richter <thor@[EMAIL PROTECTED]
> writes:
> Anthony Williams wrote:
>> Thomas Richter <thor@[EMAIL PROTECTED]
> writes:
>>
>>> Brian Cole wrote:
>>>> I know that static initialization is not considered threadsafe from
>>>> previous posts on this list. However, if a function static is POD
>>>> being initialized by a function call then the value should always be
>>>> initialized correctly, even if a inconsequential race condition
>>>> occurs. This assumes that POD assignments are atomic, what platforms
>>>> is this not true on?
>>> All I know of. For example,
>>>
>>> struct foo {
>>> int a,b;
>>> };
>>>
>>> static struct foo f = {1,2};
>>>
>>> is a POD initialization, and if two threads run into this line of
>>> code, it may happen that thread A initializes f.b while thread B is
>>> already using f.b for something else, so no, this is definitely not
>>> safe.
>>
>> Wrong. Initialization of a POD struct with an aggregate initializer
>> consisting only of constant expressions is static initialization and
>> must happen before the execution of the program begins.
>
>
> Is this guaranteed?
It is for non-local objects (file statics, class static members and
namespace-scope variables). See the C++ Standard 3.6.2p1:
"Zero-initialization and initialization with a constant expression are
collectively called static initialization; all other initialization is
dynamic initialization. Objects of POD types (3.9) with static stor-
age duration initialized with constant expressions (5.19) shall be
initialized before any dynamic initialization takes place."
The guarantee for local statics is not quite as strict.
C++ Standard 6.7p4: "A local object of POD type (3.9) with static
storage duration initialized with constant-expressions is initialized
before its block is first entered. "
Every compiler I've seen does this (for PODs only) by initializing
before the program begins, with constants in the data
segment. Non-PODs are initialized "the first time control p*****
through its declaration", which is usually unsynchronized, and
is therefore dangerous with multiple threads.
> Things *might* be better with const, they definitely are for g++, but
> once again, I cannot find this requirement in the C standard.
C99 5.1.2:
"All objects with static storage duration shall be initialized (set to
their
initial values) before program startup. "
That's quite unambiguous.
Anthony
--
Anthony Williams | Just Software Solutions Ltd
Custom Software Development | http://www.justsoftwaresolutions.co.uk
Registered in England, Company Number 5478976.
Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL


|