V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
chenqh
V2EX  ›  Go 编程语言

golang 的 tomb 怎么起作用的?

  •  
  •   chenqh · 2021-05-18 21:41:03 +08:00 · 1052 次点击
    这是一个创建于 1065 天前的主题,其中的信息可能已经有所发展或是发生改变。

    kill 的内部实现是这个样子

    
    func (t *Tomb) kill(reason error) {
    	if reason == ErrStillAlive {
    		panic("tomb: Kill with ErrStillAlive")
    	}
    	if reason == ErrDying {
    		if t.reason == ErrStillAlive {
    			panic("tomb: Kill with ErrDying while still alive")
    		}
    		return
    	}
    	if t.reason == ErrStillAlive {
    		t.reason = reason
    		close(t.dying)
    		for _, child := range t.child {
    			child.cancel()
    		}
    		t.child = nil
    		return
    	}
    	if t.reason == nil {
    		t.reason = reason
    		return
    	}
    }
    

    那按理来讲 <-tomb.Dying() 应该永远 block 才对呀

    看不明白

    1 条回复    2021-05-19 09:01:55 +08:00
    buffzty
        1
    buffzty  
       2021-05-19 09:01:55 +08:00
    你看一下 puxu 里面是咋写的 都有注释 看了你就明白了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5816 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 06:06 · PVG 14:06 · LAX 23:06 · JFK 02:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.