Home >Backend Development >Golang >Why Does Go Create Few Threads When Many Goroutines Block on Fast File Writes?

Why Does Go Create Few Threads When Many Goroutines Block on Fast File Writes?

Susan Sarandon
Susan SarandonOriginal
2024-12-16 06:02:11839browse

Why Does Go Create Few Threads When Many Goroutines Block on Fast File Writes?

Why Minimal Thread Creation Occurs with File Write Blockage in Go

In Go, when goroutines engage in blocking calls, threads are typically created to facilitate such operations. However, a puzzling observation arises when multiple goroutines are blocked while attempting to write to a file. Despite the presence of numerous goroutines, a limited number of threads are established.

Test Code and Observations

The following test script demonstrates this behavior:

package main

import (
    "io/ioutil"
    "os"
    "runtime"
    "strconv"
)

func main() {
    runtime.GOMAXPROCS(2)
    data, err := ioutil.ReadFile("./55555.log")
    if err != nil {
        println(err)
        return
    }
    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 {}
}

Ironically, when this script is executed, only a handful of threads are created, as indicated by the command:

$ cat /proc/9616/status | grep -i thread
Threads:    5

Resolving the Conundrum

The key to understanding this behavior lies in the nature of the blocking operation. In the original test, the file writes are completing too swiftly to trigger the creation of new threads.

To illustrate this, the test script was modified to write a much larger block of data:

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 modification resulted in the creation of over 200 threads, as shown by the command:

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

Conclusion

Thus, when file write operations are performed quickly, the blocking nature of the calls may not be sufficient to trigger the creation of multiple threads. Only when the blocking operations become more substantial will the need for additional threads become apparent, resulting in the proliferation of threads as expected.

The above is the detailed content of Why Does Go Create Few Threads When Many Goroutines Block on Fast File Writes?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn