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.
> Even if you return a copy of f from a function:
>
> struct foo init_me(void)
> {
> static struct foo f = {1,2};
>
> return f;
> }
This too is safe.
> void bar(void)
> {
> static struct foo = bla();
> }
>
> might call bla() once or several times, and might leave a second
> thread with a partially initialized "foo" because a first thread
> already set the "is initialized" flag on entry before the value has
> been copied over completely.
>
> In short: Don't do that.
Totally agree here. Dynamic initialization is not safe.
>> For example:
>>
>> unsigned int Bar(); // assumed to be a thread safe function that
>> returns the same value every time
>> void Foo()
>> {
>> static unsigned int value = Bar();
>> }
>
> Here you are returning an "int", not only a POD. On some
> architectures, "int"s can be accessed "atomically", probably under
> certain constraints (i.e. alignment), but once again, this is not a
> construction that you should depend on.
Agree that this is still not safe: it's still dynamic initialization.
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


|