Why does it not create many threads when many goroutines are blocked in writing file in golang?

后端 未结 3 637
盖世英雄少女心
盖世英雄少女心 2020-12-05 16:01

As we know in go, a thread may be created when the goroutine has to perform a blocking call, such as a system call, or a call to a C library via cgo. Some test code:

3条回答
  •  一生所求
    2020-12-05 16:52

    I altered your program slightly to output a much bigger block

    package main
    
    import (
        "io/ioutil"
        "os"
        "runtime"
        "strconv"
    )
    
    func main() {
        runtime.GOMAXPROCS(2)
        data := make([]byte, 128*1024*1024)
        for i := 0; i < 200; i++ {
            go func(n int) {
                for {
                    err := ioutil.WriteFile("testxxx"+strconv.Itoa(n), []byte(data), os.ModePerm)
                    if err != nil {
                        println(err)
                        break
                    }
                }
            }(i)
        }
        select {}
    }
    

    This then shows >200 threads as you expected

    $ cat /proc/17033/status | grep -i thread
    Threads:    203
    

    So I think the syscalls were exiting too quickly in your original test to show the effect you were expecting.

提交回复
热议问题