Go怎么没有stackoverflows [英] How come Go doesn't have stackoverflows

查看:154
本文介绍了Go怎么没有stackoverflows的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此演示文稿中阅读了 http://golang.org/doc/ExpressivenessOfGo.pdf 第42页:


安全

- 没有堆栈溢出


这怎么可能?和/或Go如何工作以避免这种情况?

解决方案

这是一个称为分段堆栈的功能:每个goroutine都有自己的堆栈,在堆上分配



在最简单的情况下,编程语言实现为每个进程/地址空间使用一个堆栈,通常使用称为 push pop的特殊处理器指令进行管理(或类似的东西),并实现为从固定地址(通常是虚拟内存顶部)开始的动态栈帧数组。


<这是(或曾经是)很快,但不是特别安全。当许多代码在同一个地址空间(线程)中同时执行时,会造成麻烦。现在每个人都需要自己的堆栈。但是,所有的堆栈(除了一个堆栈)都必须是固定大小的,以免它们彼此重叠或与堆栈重叠。



任何使用堆栈的编程语言但是,也可以通过以不同的方式管理堆栈来实现:通过使用列表数据结构或类似的存储堆栈帧,但实际上分配在堆上。没有堆栈溢出,直到堆填满。


I read in this presentation http://golang.org/doc/ExpressivenessOfGo.pdf page 42:

Safe

- no stack overflows

How is this possible? and/or how does Go works to avoid this?

解决方案

It's a feature called "segmented stacks": every goroutine has its own stack, allocated on the heap.

In the simplest case, programming language implementations use a single stack per process/address space, commonly managed with special processor instructions called push and pop (or something like that) and implemented as a dynamic array of stack frames starting at a fixed address (commonly, the top of virtual memory).

That is (or used to be) fast, but is not particularly safe. It causes trouble when lots of code is executing concurrently in the same address space (threads). Now each needs its own stack. But then, all the stacks (except perhaps one) must be fixed-size, lest they overlap with each other or with the heap.

Any programming language that uses a stack can, however, also be implemented by managing the stack in a different way: by using a list data structure or similar that holds the stack frames, but is actually allocated on the heap. There's no stack overflow until the heap is filled.

这篇关于Go怎么没有stackoverflows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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