473,320 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

initialisation of static

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

Jan 21 '06 #1
5 1485
<yu***@gmx.de> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com
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;
.....
}


Yes in single threaded. Not guaranteed in multi-threaded.

http://blogs.msdn.com/oldnewthing/ar.../08/85901.aspx
--
John Carson
Jan 21 '06 #2
> Yes in single threaded. Not guaranteed in multi-threaded.

http://blogs.msdn.com/oldnewthing/ar.../08/85901.aspx


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?

Jan 21 '06 #3
Shark wrote:
Yes in single threaded. Not guaranteed in multi-threaded.

http://blogs.msdn.com/oldnewthing/ar.../08/85901.aspx


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.

Jan 21 '06 #4
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

Jan 21 '06 #5
<yu***@gmx.de> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com
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-lit...html#faq-10.12
--
John Carson
Jan 22 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tim | last post by:
Please advise if you can. Presumably initialisation of members in member initialisation lists is perfomed by 'C' run-time startup. If the CRT was never started-up would those members be garbage?...
2
by: John Carson | last post by:
One routinely hears that the order of initialisation of objects with static storage duration in different translation units cannot in general be guaranteed. I have a question about one way to...
6
by: clilley | last post by:
The following code causes a segmentation fault on DEC Tru64: foo.cc (built into libFoo.so) //--------------------------- include <iostream> bool createFoo() { std::cout << "createFoo" <<...
2
by: John Smith | last post by:
Hi, I have a question regarding the initialisation of aggregates: The C (99) standard states: section 6.7.8, paragraph 21 states: If there are fewer initializers in a brace-enclosed list than...
13
by: Frederick Gotham | last post by:
I have just been reading 8.5 in the Standard, and am trying to make sense of the different kinds of initialisations. Up until now, I thought of an object as either NOT being initialised (i.e....
4
by: softwareakash | last post by:
Hi I have a dll which logs some values in a text file. In this I am initialising some values as soon as the object gets called like this public class Logger { // Initialising variables from...
7
by: Quantum | last post by:
Hi, If I have this in a header file: //Part of the class myClass: static int myVar=99; but then in the source file have this: int myClass::myVar=77;
5
by: kselvaakumar | last post by:
I have declared a global structure like this: struct { float a; float b; }static one; This is in a separate header file file1.h; Now how can i initialise those variables one.a and one.b in...
4
by: Kevin Frey | last post by:
I have an assembly written in C++/CLI that also links in non-clr Native C++ (the C++/CLI wraps the Native C++ functionality). This assembly has an in-built tracing system that needs to be...
0
by: Anonymous | last post by:
I have a class (classA) that requires initialisation ONCE. It also provides static methods that depend on various variables etc to have been correctly initialised. Some sample code to explain...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.