Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Thread safety problems with function scope static variables vs class static private members

Reply
Thread Tools

Thread safety problems with function scope static variables vs class static private members

 
 
Hicham Mouline
Guest
Posts: n/a
 
      12-18-2008
I have a function f that is called from multiple threads
I have the choice between setting a const int array as a static local in f()

void C::f() {
static const int keys[] = { 0, ...., 15 };
}

or as a private member of C

class C {
private:
static const int keys[] ;
};
const int C::keys[] = { 0....15 };



1 In theory?
Is there a risk that keys are initialized badly because keys is a static
local variable of f (keys init'ed only first time exec enters f)
while no risk when keys is a static member of C (because keys is init'ed
before runtime passes through main even)?
I know c++03 says nothing about threads. Can one still infer anything from
it?

2 In practise, (msvc, g++, intel....)
Do these make sure keys init as function local is ok, and as a member of C
also?

regards,


 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      12-18-2008
On Dec 18, 6:41 pm, "Hicham Mouline" <(E-Mail Removed)> wrote:
> I have a function f that is called from multiple threads I
> have the choice between setting a const int array as a static
> local in f()


> void C::f() {
> static const int keys[] = { 0, ...., 15 };
> }


> or as a private member of C


> class C {
> private:
> static const int keys[] ;};


> const int C::keys[] = { 0....15 };


> 1 In theory?
> Is there a risk that keys are initialized badly because keys
> is a static local variable of f (keys init'ed only first time
> exec enters f) while no risk when keys is a static member of C
> (because keys is init'ed before runtime passes through main
> even)? I know c++03 says nothing about threads. Can one still
> infer anything from it?


No. In both cases, you have static initialization, which is
guaranteed to occur before anything else.

> 2 In practise, (msvc, g++, intel....)
> Do these make sure keys init as function local is ok, and as a
> member of C also?


As long as it is static initialization, there can be no problem.
Note that for it to be static initialization, the type in
question must be a POD, and all of the initialization
expressions must be constant expressions. But I regularly use
such things to avoid order of initialization issues.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
 
Fred Zwarts
Guest
Posts: n/a
 
      12-19-2008
"Hicham Mouline" <(E-Mail Removed)> wrote in message news:494a8b66$0$90263$(E-Mail Removed)...
>I have a function f that is called from multiple threads
> I have the choice between setting a const int array as a static local in f()
>
> void C::f() {
> static const int keys[] = { 0, ...., 15 };
> }
>
> or as a private member of C
>
> class C {
> private:
> static const int keys[] ;
> };
> const int C::keys[] = { 0....15 };
>
>
>
> 1 In theory?
> Is there a risk that keys are initialized badly because keys is a static
> local variable of f (keys init'ed only first time exec enters f)
> while no risk when keys is a static member of C (because keys is init'ed
> before runtime passes through main even)?


Who says that threads are not created before main is started?
Thread safety is required even before main in started.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      12-19-2008
On Dec 19, 9:25 am, "Fred Zwarts" <(E-Mail Removed)> wrote:
> "Hicham Mouline" <(E-Mail Removed)> wrote in
> messagenews:494a8b66$0$90263$(E-Mail Removed) .dk...
> >I have a function f that is called from multiple threads
> > I have the choice between setting a const int array as a
> > static local in f()


> > void C::f() {
> > static const int keys[] = { 0, ...., 15 };
> > }


> > or as a private member of C


> > class C {
> > private:
> > static const int keys[] ;
> > };
> > const int C::keys[] = { 0....15 };


> > 1 In theory?
> > Is there a risk that keys are initialized badly because keys
> > is a static local variable of f (keys init'ed only first
> > time exec enters f) while no risk when keys is a static
> > member of C (because keys is init'ed before runtime passes
> > through main even)?


> Who says that threads are not created before main is started?


The coding guidelines.

> Thread safety is required even before main in started.


If your coding guidelines allow creating threads in the
constructors of objects with static lifetime. Most I've seen
don't---threading is hard enough to get right without going out
of your way to make it more difficult.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      12-19-2008
James Kanze wrote:
> On Dec 18, 6:41 pm, "Hicham Mouline" <(E-Mail Removed)> wrote:
>> void C::f() {
>> static const int keys[] = { 0, ...., 15 };
>> }

>
> No. In both cases, you have static initialization, which is
> guaranteed to occur before anything else.


I thought that a static inside a function is initialized the first
time the function is called?

The above function might not be problematic with regard to thread
safety, but what about something like this:

void foo()
{
static SomeClass object;
...
}

Suppose that 'foo()' may be called from more than one thread. Can it
happen that 'object' is constructed more than once? (Worse: Can it
happen, that while the constructor of 'object' is being executed, a
second thread causes it to be called again?)

Assume that SomeClass, for example, allocates memory into a member
pointer, or does something else which would cause a serious malfunction
if the constructor were to be called more than once.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      12-19-2008
On Dec 19, 12:49 pm, Juha Nieminen <(E-Mail Removed)> wrote:
> James Kanze wrote:
> > On Dec 18, 6:41 pm, "Hicham Mouline" <(E-Mail Removed)> wrote:
> >> void C::f() {
> >> static const int keys[] = { 0, ...., 15 };
> >> }


> > No. In both cases, you have static initialization, which is
> > guaranteed to occur before anything else.


> I thought that a static inside a function is initialized the
> first time the function is called?


The standard explicitly says otherwise: "Objects of POD types
with static storage duration initialized with constant
expressions shall be initialized before any dynamic
initialization takes place." (§3.6.2/1).

In fact, in this case, the word static means what it says; such
variables are initialized statically, not dynamically.

> The above function might not be problematic with regard to
> thread safety, but what about something like this:


> void foo()
> {
> static SomeClass object;
> ...
> }


It depends on SomeClass, obviously. Does SomeClass use static
or dynamic initialization?

> Suppose that 'foo()' may be called from more than one thread.
> Can it happen that 'object' is constructed more than once?


Probably. I don't know what the next version of the standard
will say about it, but in current implementations, it's
undefined behavior to enter such a function from several
different threads without some sort of external synchronization.

But what does this have to do with static initialization?

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Static allocation in a static function & thread safety paolo.brandoli@gmail.com C++ 5 08-23-2007 09:48 PM
Can a sub-class (composite object) member function access private or protected members of base class? pkpatil@gmail.com C++ 2 06-09-2006 03:09 PM
Instantiating a static class( Class with all static members - methods and variables) SaravanaKumar Java 6 10-19-2004 08:20 AM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM
Can nested class members access private members of nesting class? CoolPint C++ 8 12-14-2003 02:30 PM



Advertisments