package main
import (
"fmt"
"sync"
)
func main() {
taskQueue := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
var wg sync.WaitGroup
var mu sync.Mutex
workerCount := 5
for i := 0; i < workerCount; i++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
for {
mu.Lock()
if len(taskQueue) == 0 {
mu.Unlock()
break
}
task := taskQueue[0]
taskQueue = taskQueue[1:]
mu.Unlock()
fmt.Printf("Goroutine %d is processing task %d\n", id, task)
fmt.Printf("Goroutine %d has finished processing task %d\n", id, task)
}
}(i)
}
wg.Wait()
}