duanqin9507 2016-02-08 05:25
浏览 29
已采纳

用频道奇怪的结果去goroutine

When i run the goroutines, i generally get 40 as value, i know its about the concurrency but why is the last number coming? I suppose the output must be:

Page number:  34  
Page number:  12  
Page number:  8  
Page number:  2  
Page number:  29

example source code:

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

func getWebPageContent(url string, c chan int, val int) interface{} {

    if r, err := http.Get(url); err == nil {
        defer r.Body.Close()
        if body, err := ioutil.ReadAll(r.Body); err == nil {
            c <- val
            return string(body)
        }
    } else {
        fmt.Println(err)
    }
    return "XoX"

}

const MAX_TH = 40

func main() {

    // pln := fmt.Println
    messages := make(chan int)
    for j := 0; j < MAX_TH; j++ {
        go func() { getWebPageContent("http://www.example.com", messages, j) }()
    }

    routine_count := 0
    var page_number int
    for {
        page_number = <-messages
        routine_count++
        fmt.Println("Page number: ", page_number)
        if routine_count == MAX_TH {
            break
        }
    }
    close(messages)
}
  • 写回答

2条回答 默认 最新

  • doukun8944 2016-02-08 06:55
    关注

    The Go Programming Language

    Frequently Asked Questions (FAQ)

    What happens with closures running as goroutines?

    Some confusion may arise when using closures with concurrency. Consider the following program:

    func main() {
        done := make(chan bool)
    
        values := []string{"a", "b", "c"}
        for _, v := range values {
            go func() {
                fmt.Println(v)
                done <- true
            }()
        }
    
        // wait for all goroutines to complete before exiting
        for _ = range values {
            <-done
        }
    }
    

    One might mistakenly expect to see a, b, c as the output. What you'll probably see instead is c, c, c. This is because each iteration of the loop uses the same instance of the variable v, so each closure shares that single variable. When the closure runs, it prints the value of v at the time fmt.Println is executed, but v may have been modified since the goroutine was launched. To help detect this and other problems before they happen, run go vet.

    To bind the current value of v to each closure as it is launched, one must modify the inner loop to create a new variable each iteration. One way is to pass the variable as an argument to the closure:

    for _, v := range values {
        go func(u string) {
            fmt.Println(u)
            done <- true
        }(v)
    }
    

    In this example, the value of v is passed as an argument to the anonymous function. That value is then accessible inside the function as the variable u.

    Even easier is just to create a new variable, using a declaration style that may seem odd but works fine in Go:

    for _, v := range values {
        v := v // create a new 'v'.
        go func() {
            fmt.Println(v)
            done <- true
        }()
    }
    

    Therefore, in your case, create a new variable by adding the statement j := j,

    for j := 0; j < MAX_TH; j++ {
        j := j
        go func() { getWebPageContent("http://www.example.com", messages, j) }()
    }
    

    For example,

    package main
    
    import (
        "fmt"
        "io/ioutil"
        "net/http"
    )
    
    func getWebPageContent(url string, c chan int, val int) interface{} {
        if r, err := http.Get(url); err == nil {
            defer r.Body.Close()
            if body, err := ioutil.ReadAll(r.Body); err == nil {
                c <- val
                return string(body)
            }
        } else {
            fmt.Println(err)
        }
        return "XoX"
    }
    
    const MAX_TH = 40
    
    func main() {
    
        // pln := fmt.Println
        messages := make(chan int)
        for j := 0; j < MAX_TH; j++ {
            j := j
            go func() { getWebPageContent("http://www.example.com", messages, j) }()
        }
    
        routine_count := 0
        var page_number int
        for {
            page_number = <-messages
            routine_count++
            fmt.Println("Page number: ", page_number)
            if routine_count == MAX_TH {
                break
            }
        }
        close(messages)
    }
    

    Output:

    Page number:  23
    Page number:  6
    Page number:  1
    Page number:  3
    Page number:  28
    Page number:  32
    Page number:  18
    Page number:  22
    Page number:  0
    Page number:  36
    Page number:  7
    Page number:  21
    Page number:  12
    Page number:  2
    Page number:  5
    Page number:  4
    Page number:  33
    Page number:  13
    Page number:  20
    Page number:  27
    Page number:  29
    Page number:  8
    Page number:  31
    Page number:  10
    Page number:  17
    Page number:  25
    Page number:  19
    Page number:  35
    Page number:  14
    Page number:  38
    Page number:  15
    Page number:  30
    Page number:  37
    Page number:  39
    Page number:  26
    Page number:  9
    Page number:  16
    Page number:  11
    Page number:  24
    Page number:  34
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号