What’s the policy for a thread to have a stack and a process to have a stack.
If we have 10 process, how many stacks we have, 10?
If we have 10 threads under one process, how many stacks we have, 1? All the thread shared the same stack?
Thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you think about what the stack is, it doesn’t make sense to share a stack.
Remember that this is a call stack not the data structure. As your instruction pointer (indicating the instruction to execute) moves through your program, it will encounter function calls which push the current context (local variables, IP before call) onto the stack before jumping to the called function. That function uses the top of the stack for its local variables, etc and when it’s done the stack gets popped leaving the local variables for your original function on the top, and restoring the IP to just after the function call.
If two threads had the same stack, they would be sharing context, but they could conceivably have different IPs. If one of the threads called a function, the stack would no longer make sense for the other thread (which is still in the original function). If the IPs are synchronised, you haven’t got two distinct threads – you’re just doing everything twice.
So as others have said: One stack per thread, per process.