aboutsummaryrefslogtreecommitdiff
path: root/tests/benchmarks/increment-mutex/stm.go
blob: 9a442a53f91468423e06496db0cee757e3e2227f (plain) (blame)
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
33
34
35
36
37
38
39
40
41
42
43
package stm

import (
	"flag"
	"sync"
)



var nFlag = flag.Int(
	"n",
	1_000,
	"The number of iterations to execute",
)

func MainTest() {
	flag.Parse()
	n := *nFlag

	for i := 0; i < n; i++ {
		v := 0
		var mu sync.Mutex
		for i := 0; i < 1000; i++ {
			go func() {
				mu.Lock()
				defer mu.Unlock()
				v++
			}()
		}

		for {
			var read int
			func() {
				mu.Lock()
				defer mu.Unlock()
				read = v
			}()
			if read == 1000 {
				break
			}
		}
	}
}