Goroutine同步

在goroutine执行的过程中,需要进行goroutine的同步。Go语言提供了sync包和channel机制来解决goroutine的同步问题

1. sync.WaitGroup

// 代码来自官方文档
package main
import (
    "fmt"
    "io/ioutil"
    "sync"
    "net/http"
)

var wg sync.WaitGroup
var urls = []string{
    "http://www.golang.org/",
    "http://www.google.com/",
    "http://www.somestupidname.com/",
}
func main() {
    for _, url := range urls {
        // Increment the WaitGroup counter.
        wg.Add(1)
        // Launch a goroutine to fetch the URL.
        go func(url string) {
            // Decrement the counter when the goroutine completes.
            defer wg.Done()
            // Fetch the URL.
            resp, err := http.Get(url)
            if err != nil {
                fmt.Println("Error is: ", err)
                return
            }
            defer resp.Body.Close()
            body, err := ioutil.ReadAll(resp.Body)
            fmt.Println(string(body))
        }(url)
    }
    // Wait for all HTTP fetches to complete.
    wg.Wait()
}


Some stupid name....
Error is:  Get http://www.google.com/: dial tcp 75.126.2.43:80: getsockopt: connection refused
Error is:  Get http://www.golang.org/: dial tcp 216.239.37.1:80: getsockopt: connection refused
// 因为没有翻墙。。。

2. chan

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

var urls = []string{
    "http://www.golang.org/",
    "http://www.google.com/",
    "http://www.somestupidname.com/",
}
var ch = make(chan string, len(urls))
func main() {
    for _, url := range urls {
        go func(url string) {
            resp, err := http.Get(url)
            if err != nil {
                str := "Error is: " + err.Error() 
                ch <- str
                return
            }
            defer resp.Body.Close()
            body, err := ioutil.ReadAll(resp.Body)
            ch <- string(body)
        }(url)
    }
    for i := 0; i < len(urls); i++ {
        str := <-ch
        fmt.Println(str)
    }
}


Some stupid name....
Error is: Get http://www.google.com/: dial tcp 173.252.102.16:80: getsockopt: connection refused
Error is: Get http://www.golang.org/: dial tcp 216.239.37.1:80: getsockopt: connection refused

channel是一种 阻塞管道 , 是自动阻塞 的. 如果 channel 满了, 对channel放入数据的操作就会阻塞, 直到有某个routine从channel中取出数据, 这个放入数据的操作才会执行. 相反同理, 如果管道是空的, 一个从channel取出数据的操作就会阻塞,直到某个routine向这个channel中放入数据, 这个取出数据的操作才会执行(原理非常类似阻塞型socket的读写缓冲区)

如果channel已经close了, 在进行读写操作会怎么样呢?
总结:多数情况下建议使用channel。处理比较简单的情况时,可以选择WaitGroup,但是处理比较复杂的逻辑时,应该使用channel。

你可能感兴趣的:(Goroutine同步)