Sobes.tech
Middle+

Kuidas on üles ehitatud goroutines ja kui palju mälu nad stackis võtavad?

sobes.tech AI

Vastus AI-lt

A goroutine is a lightweight thread of execution managed by the Go runtime. They are multiplexed onto a smaller number of system threads (thread pool).

Main components:

  • Stack: Each goroutine has a separate, expandable stack. Initially small (16KB with Go 1.4+, previously 8KB), it can grow or shrink as needed.
  • Go scheduler: Implements the M:N model (M goroutines on N system threads). It is responsible for switching between goroutines on available system threads. Switching occurs during blocking operations (input/output, waiting on a channel) or explicitly via runtime.Gosched().
  • Context: Contains a pointer to the machine code for execution, a pointer to the stack, and other metadata needed by the scheduler.

The initial size of a goroutine's stack from Go 1.4 is 16KB. This size is not fixed and can change dynamically.