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

Discussion in 'C++' started by Uenal Mutlu, Mar 14, 2005.

  1. Uenal Mutlu

    Uenal Mutlu Guest

    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
    Uenal Mutlu, Mar 14, 2005
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Mar 14, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    14
    Views:
    1,455
  2. Thomas Matthews
    Replies:
    5
    Views:
    2,369
    tom_usenet
    Aug 2, 2004
  3. indushekara
    Replies:
    4
    Views:
    572
    Alan Balmer
    Jun 22, 2005
  4. Michael Tan
    Replies:
    32
    Views:
    930
    Ara.T.Howard
    Jul 21, 2005
  5. Lars Olsson
    Replies:
    2
    Views:
    99
    Lars Olsson
    Mar 19, 2011
Loading...

Share This Page