登录
首页 >  Golang >  Go问答

函数退出后,go例程如何从调用函数中访问局部变量?

来源:stackoverflow

时间:2024-03-17 08:24:27 344浏览 收藏

在 Go 语言中,局部变量通常在函数退出后就无法访问。但是,当变量在函数内部被 goroutine 使用时,编译器会通过“逃逸分析”将变量分配到堆上,从而使变量在函数退出后仍然可用。这使得 goroutine 可以继续访问和更新变量,即使函数已经退出。

问题内容

对 golang 来说相当陌生。我对 go 的变量范围有点困惑。我有以下玩具程序

package main

import "sync"
import "time"
import "fmt"
import "math/rand"

func main() {
    go main_helper()
    time.sleep(time.duration(1000000) * time.millisecond)

}
func main_helper() {
    rand.seed(time.now().unixnano())

    count := 0
    finished := 0
    var mu sync.mutex
    cond := sync.newcond(&mu)

    for i := 0; i < 10; i++ {
        go func(i int) {
            vote := requestvote(i)
            mu.lock()
            defer mu.unlock()
            if vote {
                count++
            }
            fmt.printf("cur_count: %d\n", count)
            finished++
            cond.broadcast()
        }(i)
    }

    mu.lock()
    for count < 5 {
        cond.wait()
    }
    if count >= 5 {
        println("received 5+ votes!")
    } else {
        println("lost")
    }
    mu.unlock()
    fmt.printf("exited main loop\n")

}

func requestvote(i int) bool {
    if i > 6 {
        time.sleep(time.duration(1000) * time.millisecond)
    } else {
        time.sleep(time.duration(rand.intn(100)) * time.millisecond)
    }
    fmt.printf("go routine: %d requested vote\n", i)
    return true
}

在 go 演示中运行此示例时,我得到以下输出:

go routine: 0 requested vote
cur_count: 1
go routine: 6 requested vote
cur_count: 2
go routine: 1 requested vote
cur_count: 3
go routine: 4 requested vote
cur_count: 4
go routine: 2 requested vote
cur_count: 5
received 5+ votes!
Exited main loop
go routine: 3 requested vote
cur_count: 6
go routine: 5 requested vote
cur_count: 7
go routine: 7 requested vote
cur_count: 8
go routine: 8 requested vote
cur_count: 9
go routine: 9 requested vote
cur_count: 10

这就提出了一个问题,当 main_helper() 退出时,为什么像 countmu 这样的局部变量不会超出范围?为什么我们仍然看到未完成的 go 例程正确更新计数变量?


解决方案


这是“逃逸分析”的结果。编译器意识到变量 count 对函数 main_helper 进行转义,因为它在 goroutine 中使用,因此该变量分配在堆上而不是堆栈上。一般来说,您可以返回指向局部变量的指针,并且由于转义分析,编译器会在堆上分配该变量,例如:

type x struct {
   ...
}

func newx() *x {
  return &x{}
}

这是一种常见的类似构造函数的模式来初始化结构。此代码相当于:

func newx() *x {
  return new(x)
}

在您的程序中,count:=0 声明相当于:

count:=new(int)
*count=0

并且 goroutine 保留一个指向 count 的指针。 finished 相同。

任何符合 go 语言规范的编译器都可以使用其作者选择实现的任何技术,例如将所有变量放在堆上,根本不释放内存,使用引用计数或仅将一些变量放在堆栈上。

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《函数退出后,go例程如何从调用函数中访问局部变量?》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>