golang: goroute with select doesn't stop unless I added a fmt.Print()

前端 未结 2 1766
花落未央
花落未央 2020-12-20 18:53

I tried the Go Tour exercise #71

If it is run like go run 71_hang.go ok, it works fine.

However, if you use go run 71_hang.go nogood

2条回答
  •  鱼传尺愫
    2020-12-20 19:10

    Putting a default statement in your select changes the way select works. Without a default statement select will block waiting for any messages on the channels. With a default statement select will run the default statement every time there is nothing to read from the channels. In your code I think this makes an infinite loop. Putting the fmt.Print statement in is allowing the scheduler to schedule other goroutines.

    If you change your code like this then it works properly, using select in a non blocking way which allows the other goroutines to run properly.

        for {
            select {
            case todo := <-toDoList:
                if todo.depth > 0 && !visited[todo.url] {
                    crawling++
                    visited[todo.url] = true
                    go crawl(todo, fetcher, toDoList, doneCrawling)
                }
            case <-doneCrawling:
                crawling--
            }
            if crawling == 0 {
                break
            }
        }
    

    You can make your original code work if you use GOMAXPROCS=2 which is another hint that the scheduler is busy in an infinite loop.

    Note that goroutines are co-operatively scheduled. What I don't fully understand about your problem is that select is a point where the goroutine should yield - I hope someone else can explain why it isn't in your example.

提交回复
热议问题