多线程中的堆栈和调用堆栈之间的区别? [英] Difference between a stack and call stack in multithreading?

查看:270
本文介绍了多线程中的堆栈和调用堆栈之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读有关线程共享的所有东西的答案时,我偶然发现了术语调用栈".虽然我知道线程具有自己的栈,但它们不与其他线程共享的事实,但我不理解是什么调用栈意味着相对于线程. 我已经看到了一些答案,但是答案不是很清楚. 请阐明在多任务处理中调用堆栈的含义以及它与堆栈的区别. 问候

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

推荐答案

请说明调用堆栈的含义以及它与堆栈的区别 多任务处理中.

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.

堆栈也是一个数据结构,但最终是一个内存分配器.它以非常简单,恒定时间,对称的pushpop样式分配和分配内存的方式,为分配给诸如调用堆栈之类的线程的线程分配内存.

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.

简而言之,您可能认为它就像std::vectorstd::allocator之间的关系. std::vector严格来说是一种数据结构. std::allocator为它分配内存(通常确实包含幕后的数据结构,但是仅用于内存管理的数据结构).但是std::vector不一定必须使用std::allocator.

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.

通常,调用堆栈使用线程本地堆栈来分配内存,因为它既实用,高效,又符合预期的分配/取消分配的LIFO性质,并允许每个线程拥有自己的内存空间(缓解与共享内存访问相关的瓶颈)

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).

这篇关于多线程中的堆栈和调用堆栈之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆