473,750 Members | 2,182 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

misunderstandin g 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 2272
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*******@mari ani.ws> wrote in message
news:be******** @dispatch.conce ntric.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.c om...
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.dfn cis.de
"joe martin" <no@never.com > wrote in message
news:86******** *************** *********@4ax.c om...
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
3630
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? I am looking for a good explanation. Thanks in advance
17
2353
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 oriented design (owreilly - Juval Lowy), and it was actually very nice. The book goes on about how we should use Interfaces exposure instead of classes (this is my terminology and english is not my language so I hope you understand what I'm on about...).
3
22396
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 more sense. Thanks
6
2512
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
2122
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 2002, .Net Framework 1.0 SP2. I create a new project, and add a new web form to the project. On the page properties, I set EnableViewState to False and verify that the @ Page directive has "enableViewState=false" in it. I then add a TextBox...
12
8840
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 Cint("(&HC5798A2F") returns an overflow exception.
4
6111
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
6238
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 C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
2
11712
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 leaking(thoug i konw i just donot have to).Then i look inside in time.h file(mingw) ,and i found notes say"These functions write to and return pointers to static buffers that may be overwritten by other function calls".So how is the"static buffers"...
0
8999
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8836
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8260
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6803
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4712
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3322
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.