473,500 Members | 2,212 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"extern" for the same file leads to wrong result. bug or feature?

If "extern" is specified for a variable which is already in the same cpp file
then the compiler (VS6) creates a new instance of the var in each thread.
Is this a bug or a feature?
Which is standard conform?

Example:

test.cpp:
int gi = 0;

test.cpp (ie. same file)

thread_func(...)
{
extern int gi;
gi++;
//...
}

thread1 starts and exits. gi now 1 as expected.
thread2 starts and exits. gi now has value 1. Shouldn't it be 2 ?

If the "extern" declaration is not used then gi is 2.
Or, if the "extern" declaration is used and gi is moved to another cpp
then too is gi 2.

What's the correct (std conform) behaviour?
// U.Mutlu
Jul 23 '05 #1
1 1218
Uenal Mutlu wrote:
If "extern" is specified for a variable which is already in the same cpp file
then the compiler (VS6) creates a new instance of the var in each thread.
Threads are not defined by C++ _language_. You will find more information
in comp.programming.threads or in microsoft.public.vc.language.
Is this a bug or a feature?
Unknown.
Which is standard conform?
Nothing. Or both. Threads are not part of the Standard, so whatever
happens is fine by it.

Example:

test.cpp:
int gi = 0;
This declares an object 'gi' that (by default) has external linkage.
test.cpp (ie. same file)

thread_func(...)
{
extern int gi;
This is another declaration of 'gi'. This 'gi' is the same 'gi' as you
declared and defined outside of any function.
gi++;
//...
}

thread1 starts and exits. gi now 1 as expected.
thread2 starts and exits. gi now has value 1. Shouldn't it be 2 ?
Unknown. Threads are not defined by the language.
If the "extern" declaration is not used then gi is 2.
Or, if the "extern" declaration is used and gi is moved to another cpp
then too is gi 2.

What's the correct (std conform) behaviour?


Either. Both. Unknown. The use of threads introduces the conditions
that C++ Standard cannot account for.

V
Jul 23 '05 #2

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

Similar topics

2
9324
by: Shea Martin | last post by:
I am trying to use a system call which takes a function ptr. My compiler won't compile the code if I give the system_call a ptr to a class member function, A::func(). To combat this, I created an...
3
2979
by: Andy | last post by:
Hi, This is just a simple hello world program. #include <iostream> using namespace std; int main() { cout<<"Hello,world\n"; }
2
3472
by: flyaflya | last post by:
I want use a c lib in vc6. so I use extern "C" to c function, include three files "test.cpp", "b.h" and "b.c": //************************************************ // test.cpp #include...
12
2692
by: G Patel | last post by:
I've seen some code with extern modifiers in front of variables declared inside blocks. Are these purely definitions (no definition) or are they definitions with static duration but external...
19
3817
by: ccwork | last post by:
Hi all, I am reading "C: A Reference Manual" 4th ed and I get lost for the "extern". It says that global object without specifying the storage-class specifier will have "extern" as the default...
5
1914
by: jchludzinski | last post by:
I have 3 files (see below: a.h, w.c, ww.c). I would like to use a single x (declared somewhere) which would global to both compilation units: w.c & ww.c. No matter where I place the "extern"...
4
6238
by: mimi | last post by:
The programmer indicates to the compiler that a function is written in a different programming language using a linkage directives.It is intuitive that extern "SomeLanguage" is used to declare...
17
3753
by: William Pursell | last post by:
I debated whether this belongs in comp.lang.c++, and finally decided that it belongs here, because it really is a C question... Suppose I have a perfectly friendly, harmless library, libfoo,...
19
391
by: Karel Van Laer | last post by:
Hi everyone, I need to be able to mix C and C++. The main program is written in C and needs to access C++ code. To be more specific it's a user defined function in fluent, but this should not...
0
7156
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
7030
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
7217
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
7248
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6924
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4941
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...
0
3117
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...
0
3115
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
688
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.