Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
32 lines (27 sloc) 472 Bytes
package main
import (
"fmt"
"math/rand"
"sync"
"time"
)
var wg sync.WaitGroup
var counter int
var mutex sync.Mutex
func main() {
wg.Add(2)
go incrementor("Foo: ")
go incrementor("Bar: ")
wg.Wait()
fmt.Println("Final counter: ", counter)
}
func incrementor(s string) {
for i := 0; i < 20; i++ {
time.Sleep(time.Duration(rand.Intn(20)) * time.Millisecond)
mutex.Lock()
counter++
fmt.Println(s, i, "Counter: ", counter)
mutex.Unlock()
}
wg.Done()
}
You can’t perform that action at this time.