dtziv24262 2016-06-24 12:42
浏览 28
已采纳

Goroutines行为

For example i have this code:

func main() {
    go myRoutine(1, channel)
    go myRoutine(2, channel)
    go myRoutine(3, channel)
    go myRoutine(4, channel)

    for i := 0; i < 4; i++ {
        fmt.Println("Returned routine:", <-channel)
    }
}

func myRoutine(chnum int, c chan int) {
    //doing some stuff here
    c <- chnum
    //and doing some stuff here
}

I want myRoutine to exit just after writing to channel. Google tells that i should have shutdown channel and signal here if i want exit my goroutine. I wanted to know isn't it enough just add return after с <- chnum?

And second question about goroutines: When i start my app it creates 4 processes (one per cpu core) and my routines add two more. Should i do something to kill this two processes after finishing goroutines or Go does it by himself?

  • 写回答

2条回答 默认 最新

  • duanfangfei5558 2016-06-25 20:45
    关注

    It's not enough to add a "return" statement after "с <- chnum". That's because the channel will block until the information can be inserted in the channel. Also, you need to consider that you might need a buffered channel so multiple entries can be inserted and retrieved from your threads. Consider the code below, which uses buffered channels:

    package main
    
    import "fmt"
    
    func main() {
    
        numGoRountines :=  10
        c := make(chan int, numGoRountines)
    
        for i := 0; i < numGoRountines; i++ {
            go myRoutine(i, c)
        }
    
        for i := 0; i < numGoRountines; i++ {
            fmt.Println("Returned routine:", <-c)
        }
    }
    
    func myRoutine(chnum int, c chan int) {
        //doing some stuff here
        c <- chnum
        //and doing some stuff here
    }
    

    Possible output:

    Returned routine: 9
    Returned routine: 0
    Returned routine: 1
    Returned routine: 2
    Returned routine: 3
    Returned routine: 4
    Returned routine: 5
    Returned routine: 6
    Returned routine: 7
    Returned routine: 8
    

    Regarding your second question, go will clean those extra goroutines for you. But be aware that if the parent dies, the child threads dies as well. You can see an example below:

    package main
    
    import (
        "fmt"
        "sync"
    )
    
    func main() {
    
        var waitGroupOnlyForParents sync.WaitGroup
    
        numGoRountines :=  4
    
        for i := 0; i < numGoRountines; i++ {
            waitGroupOnlyForParents.Add(1)
            go myRoutine(&waitGroupOnlyForParents, i)
        }
    
        // makes main function waits for every one who calls the method Done() in our wait group
        waitGroupOnlyForParents.Wait()
    }
    
    func myRoutine(wg *sync.WaitGroup, myId int) {
        defer wg.Done()
    
        numGoRountines := 2
        for i := 0; i < numGoRountines; i++ {
            go anotherRoutine(myId, i)
        }
    }
    func anotherRoutine( parentId, childId int){
        fmt.Printf("Created by %d, my name is %d
    ", parentId, childId)
    }
    

    possible output:

    Created by 3, my name is 1
    Created by 0, my name is 1
    Created by 1, my name is 1
    

    Now suppose we wish to sync all threads, to make sure each of them does everything they need we change the code slightly to this:

    package main
    
    import (
        "fmt"
        "sync"
    )
    
    func main() {
    
        var waitGroupForAllThreads sync.WaitGroup
    
        numGoRountines :=  4
    
        for i := 0; i < numGoRountines; i++ {
            waitGroupForAllThreads.Add(1)
            go myRoutine(&waitGroupForAllThreads, i)
        }
    
        // makes main function waits for every one who calls the method Done() in our wait group
        waitGroupForAllThreads.Wait()
    }
    
    func myRoutine(wg *sync.WaitGroup, myId int) {
        defer wg.Done()
    
        numGoRountines := 2
        for i := 0; i < numGoRountines; i++ {
            wg.Add(1)
            go anotherRoutine(wg, myId, i)
        }
    }
    func anotherRoutine(wg *sync.WaitGroup, parentId, childId int){
        defer wg.Done()
        fmt.Printf("Created by %d, my name is %d
    ", parentId, childId)
    }
    

    possible output (in this case all prints will for sure be displayed, the order may change):

    Created by 3, my name is 1
    Created by 0, my name is 1
    Created by 1, my name is 1
    Created by 2, my name is 1
    Created by 3, my name is 0
    Created by 0, my name is 0
    Created by 1, my name is 0
    Created by 2, my name is 0
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计