CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Do threads have their own stack?

    Or alternatively, is the stack safe in a multithreaded app (yes, I guess it must be but here's what I'm getting at - consider the following code)

    Code:
    int some_func()
    {
    static int x;
    
          x = call_some_other_func();
    
          return x;
    }
    I appreciate that if the above function was getting called from 2 different threads (without any thread synchronisation) it'd be a recipe for disaster because there's only one copy of 'x' being shared between two unsynchronised threads. But what if 'x' was a local variable...?

    Code:
    int some_func()
    {
    int x;
    
          x = call_some_other_func();
    
          return x;
    }
    Assuming that 'call_some_other_func()' was thread safe, is it safe to call the above function from two unsynchronised threads? Does each of them see a different copy of x?
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  2. #2
    Join Date
    Oct 2011
    Posts
    97

    Re: Do threads have their own stack?

    Threads share memory, and there's only one stack, so I would say the answer to your question is yes. I think you might run into problems with the static variable, but not the member variable. Threads all share the same program stack, but they keep track of what variables on the stack are theirs. So if you have 10 threads of the same function that all create a local variable 'x', there will be 10 'x' variables on the stack.

    But as I said, I only think you'll run into problems with a static variable. It's very possible that you might not.

  3. #3
    Join Date
    Jan 2009
    Posts
    596

    Re: Do threads have their own stack?

    Quote Originally Posted by Access_Denied View Post
    Threads share memory, and there's only one stack, so I would say the answer to your question is yes. I think you might run into problems with the static variable, but not the member variable. Threads all share the same program stack, but they keep track of what variables on the stack are theirs. So if you have 10 threads of the same function that all create a local variable 'x', there will be 10 'x' variables on the stack.

    But as I said, I only think you'll run into problems with a static variable. It's very possible that you might not.
    Where on earth did you hear this? Each thread has it's own stack - if it didn't you would lose the benefits of stack based allocation (i.e. last in, first out) and also where would the function call return points be stored?

    Threads do share the heap though, at least typically (though someone out there may know otherwise in some instances )

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Do threads have their own stack?

    Quote Originally Posted by John E View Post
    Code:
    int some_func()
    {
        int x;
        x = call_some_other_func();
        return x;
    }
    Assuming that 'call_some_other_func()' was thread safe, is it safe to call the above function from two unsynchronised threads?
    It is safe to call. Local variables are thread safe.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Do threads have their own stack?

    I like the mental model of "one stack per context of execution": http://cboard.cprogramming.com/cplus...ml#post1084354

    gg

  6. #6
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Do threads have their own stack?

    There is clearly stated: each thread has its own stack.
    So, if have a local non-static variable, we can look at it as to a different object in different fnction calls from different threads.
    As Paul already said, normally there is no synchronization problem in ths case.

    A static variable (we can include here the global ones) is not on the stack, then may be possible to have problems if accessing it from different threads.

    Besides well known synchronization mechanisms like critical section and so on, in Windows we can use:
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Do threads have their own stack?

    Quote Originally Posted by John E View Post
    But what if 'x' was a local variable...?

    Code:
    int some_func()
    {
    int x;
    
          x = call_some_other_func();
    
          return x;
    }
    Assuming that 'call_some_other_func()' was thread safe, is it safe to call the above function from two unsynchronised threads? Does each of them see a different copy of x?
    Considering the fact that after some_func scope is left local x is invalid and unreachable, and only returned copy remains on the stack... than yes, it is thread safe to be used in multithreaded app, as the return value exists only in the scope of a caller, and therefore no other thread is able to access it.
    Best regards,
    Igor

  8. #8
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Do threads have their own stack?

    All after all, I think it's not very clear what a stak is.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured