github.com/DARA-Project/GoDist-Scheduler@v0.0.0-20201030134746-668de4acea0d/examples/sharedIntegerChannelRandom_test/sharedIntegerChannelRandom.go (about) 1 package main 2 3 /* 4 5 This test is similar to sharedIntegerChanel, with the addition of 6 randomness. A global random seed is set from the time package, and a 7 random integer is added to the shared integer before it is written to 8 the communication channel. 9 10 The nodeterminism of the first line in main() 11 [rand.Seed(int64(time.Now().Nanosecond()))] is killed by a one line 12 change to the math/rand package where the seed of any random instance 13 is set to 0. 14 TODO : Record the value of seed instead and use the same value during replay. 15 We shouldn't be changing the random behaviour. 16 17 The second line of man instantiates an instance of a rand type also 18 using wall clock time as a seed. This nondeterminism is killed in 19 math/rng.go by also setting the seed of each rng to 0. 20 21 */ 22 23 24 import ( 25 "log" 26 "time" 27 "math/rand" 28 ) 29 30 const LOOPS = 50 31 const THREADS = 3 32 33 var shared int 34 var comm chan int 35 var r *rand.Rand 36 37 func main() { 38 rand.Seed(int64(time.Now().Nanosecond())) 39 r = rand.New(rand.NewSource(int64(time.Now().Nanosecond()))) 40 comm = make(chan int, THREADS) 41 go op(add) 42 go op(sub) 43 go op(mult) 44 //go op(div) 45 comm <- 1 46 comm <- 2 47 comm <- 3 48 log.Println("Main thread about to sleep") 49 time.Sleep(3 * time.Second) 50 log.Println(shared) 51 } 52 53 func op(oper func(int)) { 54 for i:=1;i<LOOPS;i++{ 55 val := <- comm 56 oper(val) 57 //Randomness 58 if rand.Int() % 2 == 0 { 59 comm <- shared + rand.Int() 60 } else { 61 comm <- shared + r.Int() 62 } 63 64 time.Sleep(time.Nanosecond) 65 } 66 } 67 68 func add(n int) { 69 shared += n 70 log.Println("add ->",shared,"<------------------------------------") 71 } 72 73 func sub(n int) { 74 shared -= n 75 log.Println("sub ->",shared,"<------------------------------------") 76 } 77 78 func mult(n int) { 79 shared *= n 80 log.Println("mult ->",shared,"<------------------------------------") 81 } 82