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条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改