initialisation of static

Y

yuliy

Hello,

I am not sure. If I declare inside a function a static variable I can
be sure that it is initialised exactly once, and , what is important to
me in presence of multithread environment, also only once Right?

void foo ()
{
static SomeClass a;
.....
}
 
R

Rolf Magnus

Shark said:
Ok, I am not very good at C++ but lately I have been reading (specially
one article on CUJ) that the upcoming C++ standard may include support
for threads derived from boost. Does that mean the entire
initialization system for statics will change?

Well, Standard C++ doesn't support threads currently, so it doesn't define
at all what happens when you create multiple threads. So if threads will be
added, the rules might be extended to the case of multiple threads, but I'd
say the existing ones don't need to change.
 
Y

yuliy

Thank you John Carson,
very good link, it clears all, And one more question. If I will somehow
move the code outside of function scope to global scope, will I resolve
concurrency problem?
Thanks
 
J

John Carson

Thank you John Carson,
very good link, it clears all, And one more question. If I will
somehow move the code outside of function scope to global scope, will
I resolve concurrency problem?
Thanks

If initialization takes place while your program still only has one thread,
then obviously you avoid the multi-threaded problem. A variable at global
scope will have this effect, provided you don't spawn any extra threads
until you enter main() (if, however, the constructor of a global variable
spawns a new thread, then you still have the problem).

Of course, global variables have their own problems if they appear in
multiple files.

http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top