Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Programming Threads > Re: POD functio...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 19 Topic 4068 of 4146
Post > Topic >>

Re: POD function static initialization from function return, thread

by Thomas Richter <thor@[EMAIL PROTECTED] > Oct 4, 2008 at 11:02 AM

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. Even 
if you return a copy of f from a function:

struct foo init_me(void)
{
static struct foo f = {1,2};

return f;
}

the compiler may simply test a (non-mutex) protected flag in the 
function and initialize f on the first call. If a second thread runs 
into the function, it is unclear whether f will be initialized a second 
time, or might return with a only partially initialized f instead. 
Similarly,

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.

> 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.

> Then even if Bar is invoked concurrently `value` will always be the
> same for the rest of the Foo function call. Is this true? Or am I
> playing with fire?

You're playing with fire, sorry. Use a mutex to protect the 
initialization of "foo".

Some compilers, e.g. gcc, will automatically wrap the initialization 
into a mutex if compiled with proper options (-pthread), but this is 
nothing ANSI C ensures for you, but rather a compiler extension.

So long,
	Thomas
 




 19 Posts in Topic:
POD function static initialization from function return, thread
Brian Cole <coleb2@[EM  2008-10-03 15:07:52 
Re: POD function static initialization from function return, thr
Thomas Richter <thor@[  2008-10-04 11:02:01 
Re: POD function static initialization from function return, thr
"Chris M. Thomasson&  2008-10-04 04:51:49 
Re: POD function static initialization from function return, thr
Thomas Richter <thor@[  2008-10-04 15:15:57 
Re: POD function static initialization from function return, thr
"Chris M. Thomasson&  2008-10-04 18:34:18 
Re: POD function static initialization from function return, thr
Anthony Williams <anth  2008-10-06 14:13:37 
Re: POD function static initialization from function return, thr
Thomas Richter <thor@[  2008-10-07 04:57:53 
Re: POD function static initialization from function return, thr
Anthony Williams <anth  2008-10-07 08:05:35 
Re: POD function static initialization from function return, thr
Thomas Richter <thor@[  2008-10-08 01:16:48 
Re: POD function static initialization from function return, thr
Hallvard B Furuseth <h  2008-10-06 19:42:02 
Re: POD function static initialization from function return, thr
David Schwartz <davids  2008-10-07 11:11:24 
Re: POD function static initialization from function return, thr
Thomas Richter <thor@[  2008-10-08 01:20:28 
Re: POD function static initialization from function return, thr
Anthony Williams <anth  2008-10-08 08:18:27 
Re: POD function static initialization from function return, thr
Brian Cole <coleb2@[EM  2008-10-07 11:31:54 
Re: POD function static initialization from function return, thr
Anthony Williams <anth  2008-10-08 08:13:53 
Re: POD function static initialization from function return, thr
David Schwartz <davids  2008-10-08 19:47:13 
Re: POD function static initialization from function return, thr
Thomas Richter <thor@[  2008-10-11 01:44:35 
Re: POD function static initialization from function return, thr
David Schwartz <davids  2008-10-08 19:50:33 
Re: POD function static initialization from function return, thr
David Schwartz <davids  2008-10-10 21:24:11 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Nov 22 7:40:39 CST 2008.