Reputation: 24759
The tutorials and documentation I've read all say that goroutines are "not quite threads" or "lightweight threads" but can usually be treated like they are their own separate thread.
So... What are they really?
Upvotes: 5
Views: 5340
Reputation: 7782
This Morsing's blog post about the Go scheduler is nice because it has pictures with triangles, squares and circles.
From the scheduler's point of view :
The goroutine includes the stack, the instruction pointer and other information important for scheduling.
Upvotes: 0
Reputation: 30097
A few things distinguish goroutines from typical OS threads:
go
statement itself, there are channel types and operations and select
statements for coordinating the goroutines.for{}
loop will never be switched away from).They're closely related to lots of other terms:
Upvotes: 23
Reputation: 7733
When having two CPUs, goroutine(s) runs as real threads. When having single CPU, the goroutine(s) runs as co-routine of single thread that running with switching their contexts. goroutine doesn't stick the fixed thread. So it doesn't identifier like thread-id. If you want to stick goroutine as OS thread, you need to use runtime.LockOSThread()
.
Upvotes: 1