Junior — Middle
How to implement coordination and task distribution among multiple goroutines on multi-core systems?
sobes.tech AI
Answer from AI
In Go, the following mechanisms are commonly used for coordination and task distribution among multiple goroutines on multi-core systems:
- Channels: allow safe data transfer between goroutines, providing synchronization.
- Goroutine pools: create a limited number of goroutines that take tasks from a shared queue.
syncpackage: provides synchronization primitives such asWaitGroupfor waiting for goroutines to finish.
Example of task distribution using channels and WaitGroup:
package main
import (
"fmt"
"sync"
)
func worker(id int, jobs <-chan int, wg *sync.WaitGroup) {
defer wg.Done()
for job := range jobs {
fmt.Printf("Worker %d processing job %d\n", id, job)
// task processing
}
}
func main() {
jobs := make(chan int, 10)
var wg sync.WaitGroup
// Launch 3 workers
for w := 1; w <= 3; w++ {
wg.Add(1)
go worker(w, jobs, &wg)
}
// Send 5 tasks
for j := 1; j <= 5; j++ {
jobs <- j
}
close(jobs) // Close the channel so workers can finish
wg.Wait() // Wait for all workers to finish
}
This approach efficiently utilizes all available cores, allowing goroutines to process tasks in parallel and coordinate work through channels.