Difference between a stack and call stack in multithreading?

妖精的绣舞 提交于 2019-12-07 05:36:23

问题


While reading an answer on what all stuff the threads share I stumbled upon the term "call stack".While I am aware of the fact that threads have their own stack which they dont share with other threads,I am not understanding what a call stack would mean with respect to a thread. I have seen some answers but they were not very clear. Please clarify what a call stack means and how it differs from stack in the context of multitasking. Regards


回答1:


Please clarify what a call stack means and how it differs from stack in the context of multitasking.

The distinction is subtle, but this is the way I've come to understand it. Often people use these somewhat interchangeably but a call stack is solely a data structure. It describes a stack of function calls along with various associated state like the values of local variables, return states, etc.

The stack is also a data structure, but it's ultimately a memory allocator. It pools memory allocated for a thread to be used for things like the call stack with a very simple, constant-time, symmetrical push and pop style of allocating and deallocating memory.

You might think of it like the relation between std::vector and std::allocator, crudely speaking. std::vector is strictly a data structure. std::allocator allocates memory for it (which typically does involve a data structure behind the hood, but a data structure used solely for memory management). But std::vector doesn't necessarily have to use std::allocator.

Conceptually a call stack doesn't actually have to use the stack to allocate memory. It'd be hard to find, in practice, a compiler that ever does this. For example, a call stack could actually use the heap instead of the stack. It could actually require a linear-time memory allocation every time it wants to just push an argument for a function call. This would be horrible, but it's not incompatible with the concept of a call stack.

Typically call stacks use the thread-local stack to allocate memory since it's practical, efficient, matches the LIFO nature of allocation/deallocation expected, and allows each thread to have its own memory space (alleviating bottlenecks associated with shared memory access).




回答2:


A call stack is a stack data structure that stores information about the active subroutines of a computer program.

Where thread stack is what is the private stack of a thread and you know about it.

If thread is executing function, that current function call stack is going store into the thread stack .

These two things are essentially the same. They are both stack data structures.




回答3:


Read wikipages about call stack and multithreading.

In pure theory, a C implementation might not even use any stack. In practice, every compiled C implementation I heard about are using a call stack which is the processor stack (some processors, probably Itanium IA-64, sort-of have two machine stacks) when there is one (AFAIK, the IBM z Series mainframe don't have any hardware stack, it is a conventional use of some registers). So for most processors & ABIs (ARM, x86, x86-64, ...) the call stack is the stack and each thread has its own.



来源:https://stackoverflow.com/questions/33800462/difference-between-a-stack-and-call-stack-in-multithreading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!