Terminology:
There are many terms for “things that run concurrently” – process, thread, coroutine, POSIX thread, NPTL thread, lightweight process, …,
But these all mean slightly different things. None means exactly how Go does concurrency. So we introduce a new term: goroutine.
Definition
A goroutine is a Go function or method executing concurrently in the same address space as [...]
Terminology:
There are many terms for “things that run concurrently” – process, thread, coroutine, POSIX thread, NPTL thread, lightweight process, …,
But these all mean slightly different things. None means exactly how Go does concurrency. So we introduce a new term: goroutine.
Definition
A goroutine is a Go function or method executing concurrently in the same address space as other goroutines.
A running program consists of one or more goroutines. It’s not the same as a thread, coroutine, process, etc. It’s a goroutine.
Starting a goroutine
Invoke a function or method and say go:
func IsReady(what string, minutes int64) {
time.Sleep(minutes * 60*1e9);
fmt.Println(what, “is ready”)
}
go IsReady(”tea”, 6);
go IsReady(”coffee”, 2);
fmt.Println(”I’m waiting….”);
Prints:
I’m waiting…. (right away)
coffee is ready (2 min later)
tea is ready (6 min later)
Some simple facts
Goroutines are cheap.
Goroutines exit by returning from their toplevel function, or just falling off the end. Or
they can call runtime.Goexit(), although that’s rarely necessary.
Goroutines can run concurrently on different processors, sharing memory.
You don’t have to worry about stack size.
Stacks
In gccgo, at least for now, goroutines are pthreads.
Stacks are big. We will fix this soon (our solution requires changes to gcc itself).
In 6g, however, stacks are small (a few kB) and grow as needed. Thus in 6g, goroutines use little memory, you can have lots of them, and they can dynamically have huge stacks.
Scheduling
Goroutines are multiplexed as needed onto system threads. When a goroutine executes a blocking system call, no other goroutine is blocked.
We will do the same for CPU-bound goroutines at some point, but for now, if you want user-level parallelism you must set $GOMAXPROCS. or call runtime.GOMAXPROCS(n).
GOMAXPROCS tells the runtime scheduler how many non-syscall-blocked goroutines to run at once.
Related posts:







Leave Your Response