blob: 2233259b031b4a3f51e352da88031a7d5d1c8241 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
// concurrent2
// Make the tests pass!
// I AM NOT DONE
package main_test
import (
"sync"
"testing"
)
func TestCounter(t *testing.T) {
counter := updateCounter()
if counter != 100 {
t.Errorf("Counter should be 100, but got %d", counter)
}
}
func updateCounter() int {
var counter int
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
counter++ // Many goroutines trying to update the counter? We need some protection here!
}()
}
wg.Wait()
return counter
}
|