Processes, Threads & Goroutines
Definition
Concurrency
- In plain English
- Concurrency means structuring a program so several tasks can make progress during the same period.
- Think of it like
- It is like a chef juggling soup, toast and eggs by switching between them.
Parallelism
- In plain English
- Parallelism means tasks literally run at the same instant on different processor cores.
- Think of it like
- It is like three chefs each cooking one dish at the same moment.
Goroutine
- In plain English
- A goroutine is a lightweight task managed by the Go runtime, started by writing go before a function call.
- Think of it like
- It is like handing a small job to a kitchen helper who works alongside you.
Why it exists
Five shelter feeds each take 400 milliseconds. Fetching them one after another takes about two seconds.
Overlap those waits and the total can approach 400 milliseconds. Network delays and available resources still affect the result.
Go runtime and stack
- In plain English
- The Go runtime schedules goroutines, and each goroutine has a stack holding its active function calls.
- Think of it like
- Like a dispatcher giving each helper a small notebook for the current job.
How it works
Process and thread
- In plain English
- A process is a running program with its own memory, and a thread is a path of execution inside a process that shares that memory.
- Think of it like
- Like a restaurant and the staff working inside it.
sync.WaitGroup
- In plain English
- A WaitGroup lets one goroutine wait until a set of other goroutines has finished.
- Think of it like
- Like a tour guide counting everyone back onto the coach.
| Task | Memory | Starting cost | Managed by |
|---|---|---|---|
| Process | Separate memory | Relatively high | Operating system |
| Thread | Shares process memory | Usually higher than a goroutine | Operating system |
| Goroutine | Shares process memory | Starts with a few kilobytes of stack | Go runtime |
Main starts three goroutines, which all lead to WaitGroup Wait and then Finish.
package main
import (
"fmt"
"time"
)
func main() {
go func() { time.Sleep(20 * time.Millisecond); fmt.Println("ready") }()
}
package main
import (
"fmt"
"sync"
)
func main() {
var wg sync.WaitGroup
wg.Add(1)
go func() { defer wg.Done(); fmt.Println("ready") }()
wg.Wait()
}
Examples
Fetch five shelter lists
You need pet lists from five shelters. Each request can wait independently.
package main
import (
"fmt"
"sync"
"time"
)
func main() {
pets := make([]string, 5)
var wg sync.WaitGroup
for i := range pets {
wg.Add(1)
go func(i int) { defer wg.Done(); time.Sleep(20 * time.Millisecond); pets[i] = fmt.Sprint("shelter ", i) }(i)
}
wg.Wait()
fmt.Println(pets)
}
Each helper owns one existing slice element. Main reads the slice only after Wait returns.
The insight
Concurrency turns waiting time into overlap.
Read thirty checkpoints
You have thirty independent checkpoint files. These sleeps represent waiting for file reads.
package main
import (
"fmt"
"sync"
"time"
)
func main() {
var wg sync.WaitGroup
for i := 0; i < 30; i++ {
wg.Add(1)
go func() { defer wg.Done(); time.Sleep(10 * time.Millisecond) }()
}
wg.Wait()
fmt.Println("30 files parsed")
}
Overlapping waits can approach the slowest read. Processing, disk limits and coordination can prevent that speedup.
The insight
Independent tasks are the easiest wins.
Trade-offs
Pros
- Overlap network and disk waits.
- Use multiple cores when available.
- Start lightweight tasks.
Cons
- Shared data needs coordination.
- Completion order can vary.
Use when
- Tasks have independent inputs.
- Several tasks wait for external work.
Avoid when
- Tasks finish faster than coordination.
- Work must happen in a strict sequence.
Quick check
Takeaways
- Concurrency structures overlapping work; parallelism runs work simultaneously.
- Use go before a function call to start a goroutine.
- Use a WaitGroup to wait for helpers.