473,327 Members | 2,055 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,327 software developers and data experts.

Static variables and threads

Hi,

class A
{
<..>
};

const A& GetA()
{
static A a;
return a;
}
is the function GetA() safe in a multi-threaded application (one and
only one A is created) ? I suppose "threads" are not part of c++, but
what about the "common implementation" ?

Thanks,

Jeremie
Jul 22 '05 #1
10 1894
"Jeremie" <je*******@qqpart.com> wrote...
class A
{
<..>
};

const A& GetA()
{
static A a;
return a;
}
is the function GetA() safe in a multi-threaded application (one and
only one A is created) ? I suppose "threads" are not part of c++, but
what about the "common implementation" ?


Please ask about "common implementation" in comp.programming.threads.
C++ does not define anything related to threads and therefore there are
no guarantees one way or the other. That's probably why C++ compiler
implementers do not concern themselves with threading issues. At least
I wouldn't if I were to implement a C++ compiler.

As far as I understand threading, access to a singleton has to be made
safe (using available threading means, like critical sections, mutexes,
etc.) otherwise you run in all kinds of problems when two threads try
to simultaneously change the object or two others try to use the object's
value that can be half-way updated by the other two threads... The fact
that 'A' is declared const doesn't matter. You can still have "mutable"
data in A, or static members that are not affected by constness of any
object of that class.

Of course, if you can make sure that no clients of 'GetA' try to cast
away the constness of it and change it _and_ that no A object has any
mutable data that can change _and_ class A doesn't have any static data
members that also can change at any point, then yes, it's safe, I guess.
But can you really make sure that all those conditions are met?

Victor
Jul 22 '05 #2
Jeremie posted:
Hi,

class A
{
<..>
};

const A& GetA()
{
static A a;
return a;
}
is the function GetA() safe in a multi-threaded application (one and only one A is created) ? I suppose "threads" are not part of c++, but what about the "common implementation" ?

Thanks,

Jeremie


The very simple answer is look at your code and figure it
out.

If GetA() is called for the first time from Thread1, then
grand. If GetA() is called for the first time from Thread2,
and then Thread2 ends, will Thread1 be able to still work
with it? The answer is:
OFF-TOPIC
-JKop
Jul 22 '05 #3
Jeremie wrote:
Hi,

class A
{
<..>
};

const A& GetA()
{
static A a;
return a;
}
is the function GetA() safe in a multi-threaded application (one and
only one A is created) ?
All compilers I have used get this wrong and do not do thread safe
initialization of static function variables.

The standard does guarentee that the variable 'a' gets initialized
exactly once when GetA is called. Hence, wether the C++ standard
"knows" about threads or not, an implementation must guarentee this.

There is a gcc bug that covers this now.
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13684

It has been reported a few times.

I suppose "threads" are not part of c++, but what about the "common implementation" ?

Thanks,

Jeremie

Jul 22 '05 #4

"Jeremie" <je*******@qqpart.com> wrote in message
news:mn***********************@qqpart.com...
Hi,

class A
{
<..>
};

const A& GetA()
{
static A a;
return a;
}
is the function GetA() safe in a multi-threaded application (one and
only one A is created) ? I suppose "threads" are not part of c++, but
what about the "common implementation" ?
Your off-topic. Its precisely because A is unique that thread safety is
compromised. As already mentioned, proprietary implementations like threads,
mutexes, events, semaphores and critical sections are designed to isolate
and lock the instance of A to fix that issue although race conditions will
persist. But such a scenario is delibitating since only MS operating systems
need apply.

Which is the main reason why the folks in this newsgroup prefer not to deal
with it(thankfully so). A window implies not a rectangle or frame, but
rather a proprietary target with a proprietary message loop, which are
required to support most kinds of these proprietary, void passing threads
and processes. So multi-threading is most likely to remain off-topic.

Its not imposed by the newsgroup, rather, it's MS's decision purely.

Thanks,

Jeremie

Jul 22 '05 #5
JKop a exposé le 24/07/2004 :
Jeremie posted:
Hi,

class A
{
<..>
};

const A& GetA()
{
static A a;
return a;
}
is the function GetA() safe in a multi-threaded application (one and
only one A is created) ? I suppose "threads" are not part of c++, but
what about the "common implementation" ?

Thanks,

Jeremie
The very simple answer is look at your code and figure it
out.

If GetA() is called for the first time from Thread1, then
grand. If GetA() is called for the first time from Thread2,
and then Thread2 ends, will Thread1 be able to still work
with it? The answer is:


but if GetA() is called for the first time from thread1 AND thread2 ?
is one A or two A created ? this was my question
OFF-TOPIC

Jul 22 '05 #6
Gianni Mariani a couché sur son écran :
Jeremie wrote:
Hi,

class A
{
<..>
};

const A& GetA()
{
static A a;
return a;
}
is the function GetA() safe in a multi-threaded application (one and only
one A is created) ?


All compilers I have used get this wrong and do not do thread safe
initialization of static function variables.

The standard does guarentee that the variable 'a' gets initialized exactly
once when GetA is called. Hence, wether the C++ standard "knows" about
threads or not, an implementation must guarentee this.

There is a gcc bug that covers this now.
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13684

It has been reported a few times.


Thanks

Jeremie
Jul 22 '05 #7
Jeremie posted:
but if GetA() is called for the first time from thread1 AND thread2 ?
is one A or two A created ? this was my question

Platform-specific.
WinXP : Don't know
Linux : Don't know
Unix : Don't know
MSDOS version 3 : Don't know
Most likely once, and it's available until the entire process is terminated,
which occurs when there's no threads left.

-JKop
Jul 22 '05 #8
It is possible you will find this of interest...

http://blogs.msdn.com/oldnewthing/ar.../08/85901.aspx
Jul 22 '05 #9

SaltPeter wrote: ...

Clap clap.

regards,
alexander.
Jul 22 '05 #10
Jeremie,

It looks to me that this is NOT a thread safe construct.

If you have two threads, thread1 and thread2, thread1 calls
GetA() and is preempted during the construction of the static
Object A, then thread2 calls GetA(), thread2 now accesses a partially
constructed A and the results are undefined.

You need to guarantee that A will be completely constructed before
a second thread accesses it. If you construct A in the main thread, before
the worker threads start, this will work, or use some kind of thread sync
objects to prevent subsequent threads from accessing A during its
construction.

Presumably the class A has its own synchronization mechanisms
internally ( unless of course its a read-only type object).

"Jeremie" <je*******@qqpart.com> wrote in message
news:mn***********************@qqpart.com...
Hi,

class A
{
<..>
};

const A& GetA()
{
static A a;
return a;
}
is the function GetA() safe in a multi-threaded application (one and
only one A is created) ? I suppose "threads" are not part of c++, but
what about the "common implementation" ?

Thanks,

Jeremie

Jul 22 '05 #11

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

Similar topics

2
by: Steve | last post by:
Is a static method that uses local variables thread safe (eg in a web service) In the following code assuming GetRandomValue() and DoSomethingElse() are thread safe, is the static method thread...
6
by: jacob navia | last post by:
As far as I understood the standard, non-automatic variables (static/global data) can't be cached in registers because in a multiprocessing or multi-threading environment, another thread/processor...
9
by: Clint | last post by:
Hey all - Excuse the cross-post ... I'm not sure what the appropriate newsgroup would be for this question. I have a question that I'm not quite sure how to ask. For all I know, I have the...
3
by: Amy L. | last post by:
I have a method below called "ResolveHostname" which is called from the ThreadPool.QueueUserWorkItem. My concern is with the static dnsClient class where I am calling dnsClient.Lookup( Hostname)...
4
by: Dave | last post by:
I used the following class and .aspx code below to understand how static works on variables and methods taken from...
2
by: blue | last post by:
We have an abstract class with all static methods. It makes sense to have it static because there are no member variables and the constructor is empty. Some of the methods update the SQL Server...
6
by: Stephen Walch | last post by:
Our application environment consists of three basic layers: 1. Third-party unmanaged DLLs that were written before the CLR was invented and maintain a significant amount of information (including...
15
by: Laser Lu | last post by:
I was often noted by Thread Safety declarations when I was reading .NET Framework Class Library documents in MSDN. The declaration is usually described as 'Any public static (Shared in Visual...
6
by: Olumide | last post by:
Hi - I've got a class that contains static member functions alone, all of whose arguments are passed by reference as shown below: class MySpiffyClass{ // no constructor, destructor or...
2
by: Nagrik | last post by:
Dear Group, The book of Bjarne Stroustrup in chapter 5.4.4 says the following "The word static is one of the most overused words in C and C++. For static data members it has both of the...
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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.