-
February 4th, 2012, 01:10 PM
#1
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
-
February 4th, 2012, 02:34 PM
#2
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.
-
February 4th, 2012, 03:37 PM
#3
Re: Do threads have their own stack?
Originally Posted by Access_Denied
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 )
-
February 4th, 2012, 04:44 PM
#4
Re: Do threads have their own stack?
Originally Posted by John E
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
-
February 5th, 2012, 12:40 AM
#5
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
-
February 5th, 2012, 02:28 AM
#6
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:
-
February 5th, 2012, 06:12 AM
#7
Re: Do threads have their own stack?
Originally Posted by John E
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
-
February 7th, 2012, 08:51 AM
#8
Re: Do threads have their own stack?
All after all, I think it's not very clear what a stak is.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|