package main import . "fmt" import "sync" var s int func add(c chan int, m *sync.Mutex) { m.Lock() s += 1 m.Unlock() c <- 0 } func main() { var m sync.Mutex c := make(chan int) /* Creiamo un milione di goroutine che incrementano s di 1. Notate che il Mutex รจ passato per puntatore. */ for i := 0;i < 1000000; i++ { go add(c, &m) } /* Aspettiamo che tutte le goroutine siano terminate. */ for i := 0; i < 1000000; i++ { <- c } Println(s) }