当一个匿名函数在Go语言中永远等待通道时会发生什么?

huangapple go评论84阅读模式
英文:

What happens when an anonymous function waits on channel forever in golang?

问题

我有一个创建无缓冲通道的函数。该函数继续创建几个并发的匿名函数,这些函数将数据写入该通道。然后,该函数等待通道上的输入,然后返回该值。

以下是示例代码:

package main

import (
	"time"
	"fmt"
	"strconv"
	"math/rand"
)

func main() {
	for {
		text := foo()
		fmt.Println(text)
		time.Sleep(time.Second)
	}

}

func foo() string {
	ch := make(chan string)
	for i := 0; i < 10; i++ {
		// 创建一些线程
		go func(i int) {
			time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
			ch <- strconv.Itoa(i)
		}(i)
	}
	return <-ch
}

即使整个函数(示例中的foo函数)已经"死亡",那些仍在等待通道的匿名函数会发生什么情况呢?

它们会被垃圾回收,还是它们将永远徘徊在计算机内存的边缘(或者直到我终止主线程),在继续发送它们的最后一条消息之前不断消耗内存?

英文:

I have a function creating a channel with no buffer. This function goes on to create several other concurrent anonymous functions writing to said channel. The function then goes on to wait for input on the channel and then returns the value.

See example below
<!-- language: lang-go -->

package main

import (
	&quot;time&quot;
	&quot;fmt&quot;
	&quot;strconv&quot;
	&quot;math/rand&quot;
)

func main() {
	for{
		text := foo()
		fmt.Println(text)
		time.Sleep(time.Second)
	}

}

func foo() string {    
	ch := make(chan string)
	for i := 0; i &lt; 10; i++ {
        // Create some threads
		go func(i int) {
			time.Sleep(time.Duration(rand.Intn(1000))*time.Millisecond)
			ch &lt;- strconv.Itoa(i)
		}(i)
	}
	return &lt;- ch
}

What happens with the anonymous functions that are still waiting on the channel, even though the entire function (foo in example) is "dead"?

Will they be collected as garbage, or will they forever wander the limbo of my computers memory (or until I kill the main thread) eating away at it in a desperate attempt to send their last message before passing on?

答案1

得分: 3

你有一个goroutine泄漏的问题。

package main

import (
	"fmt"
	"math/rand"
	"runtime"
	"strconv"
	"time"
)

func main() {
	for {
		text := foo()
		fmt.Println(text, "NumGoroutine", runtime.NumGoroutine())
		time.Sleep(time.Second)
	}
}

func foo() string {
	ch := make(chan string)
	for i := 0; i < 10; i++ {
		// 创建一些线程
		go func(i int) {
			time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
			ch <- strconv.Itoa(i)
		}(i)
	}
	return <-ch
}

输出:

$ go run leak.go
4 NumGoroutine 10
4 NumGoroutine 20
0 NumGoroutine 28
8 NumGoroutine 37
2 NumGoroutine 46
8 NumGoroutine 55
2 NumGoroutine 64
3 NumGoroutine 73
8 NumGoroutine 82
1 NumGoroutine 91
4 NumGoroutine 100
<<--SNIP-->>
4 NumGoroutine 4006
7 NumGoroutine 4015
6 NumGoroutine 4024
9 NumGoroutine 4033
9 NumGoroutine 4042
9 NumGoroutine 4051
1 NumGoroutine 4060
0 NumGoroutine 4069
4 NumGoroutine 4078
0 NumGoroutine 4087
6 NumGoroutine 4096
^Csignal: interrupt
$

类似于内存泄漏,goroutine泄漏也是不好的。

有关背景信息,请参见Go: proposal: runtime: garbage collect goroutines blocked forever #19702: Closed.

英文:

You have a goroutine leak.

package main

import (
	&quot;fmt&quot;
	&quot;math/rand&quot;
	&quot;runtime&quot;
	&quot;strconv&quot;
	&quot;time&quot;
)

func main() {
	for {
		text := foo()
		fmt.Println(text, &quot;NumGoroutine&quot;, runtime.NumGoroutine())
		time.Sleep(time.Second)
	}
}

func foo() string {
	ch := make(chan string)
	for i := 0; i &lt; 10; i++ {
		// Create some threads
		go func(i int) {
			time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
			ch &lt;- strconv.Itoa(i)
		}(i)
	}
	return &lt;-ch
}

Output:

$ go run leak.go
4 NumGoroutine 10
4 NumGoroutine 20
0 NumGoroutine 28
8 NumGoroutine 37
2 NumGoroutine 46
8 NumGoroutine 55
2 NumGoroutine 64
3 NumGoroutine 73
8 NumGoroutine 82
1 NumGoroutine 91
4 NumGoroutine 100
&lt;&lt;--SNIP--&gt;&gt;
4 NumGoroutine 4006
7 NumGoroutine 4015
6 NumGoroutine 4024
9 NumGoroutine 4033
9 NumGoroutine 4042
9 NumGoroutine 4051
1 NumGoroutine 4060
0 NumGoroutine 4069
4 NumGoroutine 4078
0 NumGoroutine 4087
6 NumGoroutine 4096
^Csignal: interrupt
$

Like a memory leak, a goroutine leak is bad.

For some background, see Go: proposal: runtime: garbage collect goroutines blocked forever #19702: Closed.

huangapple
  • 本文由 发表于 2017年4月19日 19:03:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/43493918.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定