Mis vahe on protsessil, lõngal ja gorutinil?
sobes.tech AI
Vastus AI-lt
A process is an isolated execution unit with its own address space, resources, and memory. Each process operates independently, and inter-process communication (IPC) is commonly used for data exchange between processes.
A thread is a lightweight execution flow within a process, sharing memory and resources with other threads of the same process. Threads enable multiple tasks to run in parallel within a single process.
A goroutine is an abstraction of a lightweight thread in the Go language. Goroutines are much lighter in resources than system threads and are managed by the Go runtime scheduler. They make writing concurrent code simpler and more efficient than using regular threads.
Example of creating a goroutine in Go:
func sayHello() {
fmt.Println("Hello from goroutine")
}
func main() {
go sayHello() // start the goroutine
time.Sleep(time.Second) // wait for the goroutine to finish
}