正如我从 golang 文档中了解到的,如果我将 runtime.GOMAXPROCS(8) 设置为 8 核(intel i7)的 cpu,然后启动一个无限循环的 goroutine,其他 gorutines 不应该被阻塞,因为有足够的线程和 goprocs。但是在使用 net/http 包时就不是这样了,一个无限循环的 goroutine 会在几次调用后阻塞 http 服务器。任何人都可以帮助解释原因吗?
服务器代码:
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 …Run Code Online (Sandbox Code Playgroud) 根据Rust编程语言:
在Rust中,您可以指定在值超出范围时运行特定的代码位,编译器将自动插入此代码
程序员不应该显式地释放资源(drop从Drop特征中调用函数),drop每当所有者超出范围时Rust都会调用,这在编译期间完成,但是drop如果它依赖于Rust,Rust怎么可能知道何时调用关于运行时信息?
extern crate rand;
use rand::Rng;
struct Foo {}
impl Drop for Foo {
fn drop(&mut self) {
println!("drop occurs");
}
}
fn main() {
let foo = Foo {};
if rand::thread_rng().gen() {
let _t = foo; // move foo to _t
} // 1) drop occurs here if random bool is true
} // 2) drop occurs here if random bool …Run Code Online (Sandbox Code Playgroud)