github.com/DARA-Project/GoDist-Scheduler@v0.0.0-20201030134746-668de4acea0d/examples/SharedIntegerNoLocksProperty/sharedIntegerNoLocks.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "time" 6 "runtime" // Import for DaraLog to report names of variables + values of events 7 ) 8 9 const LOOPS = 50 10 11 var SharedVariable int 12 13 func main() { 14 // Creates a goroutine that performs the add function 50 times 15 go op(add) 16 // Creates a goroutine that performs the sub function 50 times 17 go op(sub) 18 // Creates a goroutine that performs the mul function 50 times 19 go op(mult) 20 // Sleeps for 10s 21 time.Sleep(10 * time.Second) 22 // Prints the final value of SharedVariable. 23 fmt.Println("---------Final value was",SharedVariable) 24 } 25 26 func op(oper func(int, int)) { 27 for i:=1;i<LOOPS;i++{ 28 oper(1, i) 29 } 30 } 31 32 // Adds the value 'n' to the SharedVariable variable then sleeps for 1ms 33 func add(n int, i int) { 34 SharedVariable += n 35 fmt.Println("--------Iteration", i," Add value is :", SharedVariable) 36 runtime.DaraLog("Add", "main.SharedVariable", SharedVariable) 37 time.Sleep(time.Millisecond) 38 } 39 40 // Subtracts the value 'n' from the SharedVariable variable then sleeps for 1ms 41 func sub(n int, i int) { 42 SharedVariable -= n 43 fmt.Println("--------Iteration", i, " Sub value is :", SharedVariable) 44 runtime.DaraLog("Sub", "main.SharedVariable", SharedVariable) 45 time.Sleep(time.Millisecond) 46 } 47 48 // Multiplies the SharedVariable variable with the value 'n' then sleeps for 1ms 49 func mult(n int, i int) { 50 SharedVariable *= n 51 fmt.Println("--------Iteration", i, " Mult value is :", SharedVariable) 52 runtime.DaraLog("Mult", "main.SharedVariable", SharedVariable) 53 time.Sleep(time.Millisecond) 54 } 55