a static local variable in a static method is thread local storage?

Discussion in 'C++' started by Patrick Hoffmann, Aug 7, 2003.

  1. hi folx,

    today I had "multithreading" trouble with the following code (MS
    VC++6/optimized compiling):

    ---------------------------
    class myclass
    {
    static int s_nNext;
    public:
    static int getN() { static int N = s_nNext++; return N; }
    };

    int myclass::s_nNext = 0;
    ---------------------------

    myclass::getN() increments myclass::s_nNext for one time and returns
    it before.
    With this code I wanted to create an automatic "class-id-enumeration".
    It worked well in a multi threaded enviroment with linux/gcc for a
    long time. But today I got problems under VC6++/Windows, because in
    this case N seems to be thread local storage. So every thread
    (including the main) gets a new id for the same class.

    I worked around that problem, but I'm interested in an official
    documentation of this issue. Is it part of C++, MS or VC++?

    Can anybody give me some acknowlege?

    thanx much, keep hack'n!

    p@
    Patrick Hoffmann, Aug 7, 2003
    #1
    1. Advertising

  2. Patrick Hoffmann

    Jack Klein Guest

    On 7 Aug 2003 15:24:52 -0700, (Patrick Hoffmann)
    wrote in comp.lang.c++:

    > hi folx,
    >
    > today I had "multithreading" trouble with the following code (MS
    > VC++6/optimized compiling):


    [snip]

    You need to take this to one of Microsoft's support groups in the
    news:microsoft.public.vc.* family. We discuss the C++ language here,
    which has no support at all for multithreading or thread local
    storage. These are all compiler specific issues.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
    Jack Klein, Aug 8, 2003
    #2
    1. Advertising

  3. Jack Klein <> wrote in message news:<>...
    >
    > You need to take this to one of Microsoft's support groups in the
    > news:microsoft.public.vc.* family. We discuss the C++ language here,
    > which has no support at all for multithreading or thread local
    > storage. These are all compiler specific issues.


    Ok, this is correct, if MS is the problem. But I'm not sure if this is
    right. It also could be an issue of C++ static inline methods. May be
    the compiler generates the static local variable N for every call of
    f(), because it's static & inline?

    p@
    Patrick Hoffmann, Aug 8, 2003
    #3
  4. > class myclass
    > {
    > static int s_nNext;
    > public:
    > static int getN() { static int N = s_nNext++; return N; }
    > };
    >
    > int myclass::s_nNext = 0;


    In a multithreading environment your code will generate random results.
    "myclass::s_nNext" wont be in TLS under windows,
    since you ommited __declspec(thread).

    Christian
    Christian Janßen, Aug 8, 2003
    #4
    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. Peter van Merkerk

    static variable and stack storage

    Peter van Merkerk, Aug 25, 2003, in forum: C++
    Replies:
    1
    Views:
    5,007
    Eric Liu
    Aug 26, 2003
  2. Pavan
    Replies:
    3
    Views:
    428
    Malcolm
    May 28, 2005
  3. sarathy
    Replies:
    2
    Views:
    660
    sarathy
    Jul 17, 2006
  4. Replies:
    2
    Views:
    2,349
    Rolf Magnus
    Feb 27, 2006
  5. kiluyar
    Replies:
    1
    Views:
    766
    Alf P. Steinbach
    May 22, 2007
Loading...

Share This Page