github.com/DARA-Project/GoDist-Scheduler@v0.0.0-20201030134746-668de4acea0d/examples/SimpleMutex_test/SimpleMutex.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "sync" 6 "time" 7 ) 8 9 const LOOPS = 50 10 11 var ( 12 mux sync.Mutex 13 shared int 14 ) 15 16 func main() { 17 go op(add) 18 go op(sub) 19 time.Sleep(10 * time.Second) 20 fmt.Println("----------------Final Value is :", shared) 21 } 22 23 func op(oper func(int, int)) { 24 for i:=1;i<LOOPS;i++{ 25 oper(1,i) 26 } 27 } 28 29 func add(n int, i int) { 30 mux.Lock() 31 shared += n 32 fmt.Println("--------Iteration", i," Add value is :", shared) 33 mux.Unlock() 34 } 35 36 func sub(n int, i int) { 37 mux.Lock() 38 shared -= n 39 fmt.Println("--------Iteration", i, " Sub value is :", shared) 40 mux.Unlock() 41 }