正如我从golang docs那里学到的,如果我用8个核心(intel i7)的cpu设置runtime.GOMAXPROCS(8),然后启动无限循环的goroutine,其他gorutines不应该被阻止,因为有很多线程和goprocs.但是在使用net / http包时不是这样,无限循环goroutine会在几次调用后阻塞http服务器.
任何人都可以帮忙解释原因吗?
>如果我评论“go infinite loop”行,在服务器后启动客户端,客户端将输出1000个星号;但如果我启用goroutine,客户端将在打印几个星号后阻止
>我试过在goroutine中添加runtime.LockOSThread(),似乎不起作用
>我的环境:osx 10.10,go版本go1.3.1 darwin / amd64
服务器代码:
package main
import (
"fmt"
"log"
"net/http"
"runtime"
)
func myHandler(w http.ResponseWriter,req *http.Request) {
w.Write([]byte("hello"))
}
func infiniteloop() {
for {
}
}
func main() {
// set max procs for multi-thread executing
runtime.GOMAXPROCS(runtime.Numcpu())
// print GOMAXPROCS=8 on my computer
fmt.Println("GOMAXPROCS=",runtime.GOMAXPROCS(-1))
http.Handle("/",http.HandlerFunc(myHandler))
// uncomment below line cause server block after some requests
// go infiniteloop()
if err := http.ListenAndServe(":8280",nil); err != nil {
log.Fatal(err)
}
}
客户代码:
package main
import (
"fmt"
"net/http"
)
func getOnce() {
if resp,err := http.Get("http://localhost:8280"); err != nil {
fmt.Println(err)
return
} else {
defer func() {
if err := resp.Body.Close(); err != nil {
fmt.Println(err)
}
}()
if resp.StatusCode != 200 {
fmt.Println("error codde:",resp.StatusCode)
return
} else {
fmt.Print("*")
}
}
}
func main() {
for i := 1; i < 1000; i++ {
getOnce()
if i%50 == 0 {
fmt.Println()
}
}
}
现在我知道为什么这样的emtpy循环阻塞其他goroutines,但为什么runtime.LockOSThread()也无济于事?
func infiniteloop() {
// add LockOSThread will not help
runtime.LockOSThread()
for {
}
}
正如http://golang.org/pkg/runtime/#LockOSThread所述,空循环应该在独立线程中执行,而其他goroutine不应该受到busy循环的影响.我的理解有什么不对?