473,395 Members | 1,938 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,395 software developers and data experts.

misunderstanding of 'static' behavior?

Hey all,

For singleton code that looks like this:

class JoeClass
{
friend JoeClass &joeFunction();
private:
JoeClass() { someFunction();}
}

JoeClass &joeFunction()
{
static JoeClass jc;
return jc;
}

with multiple threads, JoeClass is being returned from joeFunction
without someFunction being called. To get it to work i had to put in
status flags in the constructor before and after someFunction() and
wait on them before returning in joeFunction... I don't understand
what is happening. Am I doing something wrong or is this a bug in VC++
or something.

Thanks,
Joe
Jul 19 '05 #1
5 2247
joe martin wrote:
Hey all,

For singleton code that looks like this:

class JoeClass
{
friend JoeClass &joeFunction();
private:
JoeClass() { someFunction();}
}

JoeClass &joeFunction()
{
static JoeClass jc;
return jc;
}

with multiple threads, JoeClass is being returned from joeFunction
without someFunction being called.
someFunction() is only called when the JoeClass object is being
constructed which happens exactly once before main() is called when the
static JoeClass jc; object is created that is declared in the method
JoeClass &joeFunction().
To get it to work i had to put in status flags in the constructor before and after someFunction() and
wait on them before returning in joeFunction... I don't understand
what is happening.
The order of object construction is not defined in the standard. It
sounds like you're assuming that the static JoeClass jc; is constructed
before joeFunction() is called. If you are starting threads before main
is called (or the dll/dso is loaded), stop it. Wait until main is
called before firing off any threads.

Am I doing something wrong or is this a bug in VC++ or something.
Does not sound like it but I would not bet on it either.

Thanks,
Joe


Jul 19 '05 #2

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:be********@dispatch.concentric.net...
joe martin wrote:
Hey all,

For singleton code that looks like this:

class JoeClass
{
friend JoeClass &joeFunction();
private:
JoeClass() { someFunction();}
}

JoeClass &joeFunction()
{
static JoeClass jc;
return jc;
}

with multiple threads, JoeClass is being returned from joeFunction
without someFunction being called.


someFunction() is only called when the JoeClass object is being
constructed which happens exactly once before main() is called when the
static JoeClass jc; object is created that is declared in the method
JoeClass &joeFunction().


Not correct, construction of JoeClass is delayed until the first time that
joeFunction is called, which maybe before or after main() is called.

john

Jul 19 '05 #3

"joe martin" <no@never.com> wrote in message
news:86********************************@4ax.com...
Hey all,

For singleton code that looks like this:

class JoeClass
{
friend JoeClass &joeFunction();
private:
JoeClass() { someFunction();}
}

JoeClass &joeFunction()
{
static JoeClass jc;
return jc;
}

with multiple threads, JoeClass is being returned from joeFunction
without someFunction being called. To get it to work i had to put in
status flags in the constructor before and after someFunction() and
wait on them before returning in joeFunction... I don't understand
what is happening. Am I doing something wrong or is this a bug in VC++
or something.

Thanks,
Joe


C++ doesn't have any support for threads. Presumably something like this is
happening

1) First thread enters joeFunction, starts construction of jc, marks it as
constructed but does not yet finish construction
2) Second thread enters joeFunction, sees jc marked as constructed so exits
immediately
3) First thread completes construction of jc.

VC++ is probably doing it this way to prevent multiple construction of jc,
in the event of the JoeClass constructer indirectly causing joeFunction to
be called again. But as you've found in the case of multiple threads is can
result in joeFunction returning without jc being fully constructed.

Basically if you want threads in C++ you have to do it yourself. VC++ isn't
doing anything wrong that I can see.

john
Jul 19 '05 #4
"John Harrison" <jo*************@hotmail.com> wrote in message
news:be************@ID-196037.news.dfncis.de
"joe martin" <no@never.com> wrote in message
news:86********************************@4ax.com...
Hey all,

For singleton code that looks like this:

class JoeClass
{
friend JoeClass &joeFunction();
private:
JoeClass() { someFunction();}
}

JoeClass &joeFunction()
{
static JoeClass jc;
return jc;
}

with multiple threads, JoeClass is being returned from joeFunction
without someFunction being called. To get it to work i had to put in
status flags in the constructor before and after someFunction() and
wait on them before returning in joeFunction... I don't understand
what is happening. Am I doing something wrong or is this a bug in
VC++ or something.

Thanks,
Joe


C++ doesn't have any support for threads. Presumably something like
this is happening

1) First thread enters joeFunction, starts construction of jc, marks
it as constructed but does not yet finish construction
2) Second thread enters joeFunction, sees jc marked as constructed so
exits immediately
3) First thread completes construction of jc.

VC++ is probably doing it this way to prevent multiple construction
of jc, in the event of the JoeClass constructer indirectly causing
joeFunction to be called again. But as you've found in the case of
multiple threads is can result in joeFunction returning without jc
being fully constructed.

Basically if you want threads in C++ you have to do it yourself. VC++
isn't doing anything wrong that I can see.

john


Section 6.7 p 4 of the standards says:

[after discussing some cases not relevant here]
Otherwise such an object is initialized the first time control passes
through its declaration; such an object is considered initialized upon the
completion of its initialization. If the initialization exits by throwing an
exception, the initialization is not complete, so it will be tried again the
next time control enters the declaration. If control re-enters the
declaration (recursively) while the object is being initialized, the
behavior is undefined. [Example:
int foo(int i)
{
static int s = foo(2*i); // recursive call - undefined
return i+1;
}
-end example]
If VC++ is marking the object as constructed before it is in fact
constructed, then this would seem to me to be in conflict with the
standard --- unless multi-threading can be seen as akin to a recursive call
in this respect.

The basic point remains, of course. The orginal joeFunction code is not
thread safe.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 19 '05 #5
John Harrison wrote:

Not correct, construction of JoeClass is delayed until the first time that
joeFunction is called, which maybe before or after main() is called.


Oops - you are correct....

Jul 19 '05 #6

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

Similar topics

5
by: allison | last post by:
Can someone point me to a guide on when to use static methods versus instance methods in a class? For instance, is it bad design to have a static method that creates an instance of another class? ...
17
by: Picho | last post by:
Hi all, I popped up this question a while ago, and I thought it was worth checking again now... (maybe something has changed or something will change). I read this book about component...
3
by: Dave | last post by:
Hi, Is there a general rule to use 'static' on a class member? It seems uneccessary to have to create an instance of an object just to use it's methods where declaring something as static makes...
6
by: kracchh | last post by:
Hi, the following .NET 1.1 C#-program does not terminate (opposed to what I would expect): -------- using System; using System.Threading; public class Test {
3
by: Philip Tripp | last post by:
I've read numerous sources stating that view state can be disabled per control, and per page, but can't seem to keep web form controls from remembering their state on a postback. I'm using VS.Net...
12
by: Dennis | last post by:
I get the following results: Cint(&HC5798A2F) returns -981890513 as it should and Clng("&HFFFFFFFFC5798A2F") returns -981890513 as it should. However when using a string ...
4
by: nospam_timur | last post by:
Let's say I have two files, myfile.h and myfile.c: myfile.h: int myfunction(int x); myfile.c: #include "myfile.h"
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
2
by: chenxinleo | last post by:
Hi, When i use some standard library functions and fields,which return char* type(like ctime in time.h, optarg in getopt.h)and do not have to be freed after calling,i always worry about memory...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.