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

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

问题描述

在阅读有关线程共享的所有内容的答案时,我偶然发现了调用堆栈"一词.虽然我知道线程有自己的堆栈,它们不与其他线程共享,但我不明白什么调用堆栈将意味着相对于线程.我看过一些答案,但不是很清楚.请澄清调用堆栈的含义以及它与多任务上下文中的堆栈有何不同.问候

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天全站免登陆