github.com/DARA-Project/GoDist-Scheduler@v0.0.0-20201030134746-668de4acea0d/examples/sharedIntegerChannelIterative/sharedIntegerChannel.go (about) 1 package main 2 3 /* 4 This is a test of thread non-determinism; threads communicate on a 5 shared channel, and perform operations on a shared resource. The 6 purpose of this test is to demonstrate that GoDist has the ability to 7 accuratly replay the thread schedule of a single process which 8 communicates on channels 9 10 BUG This test seems to be deterministic automatically when run with 11 GOMAXPROCS set to 1, which makes it a bad test becuase a simplifing 12 assumptions about GoDist is that the execution will be serialized i.e 13 GOMAXPROCS will allways equal 1. This fact makes this a bad test. 14 */ 15 16 17 import ( 18 "log" 19 "time" 20 "os" 21 "strconv" 22 ) 23 24 const LOOPS = 50 25 const THREADS = 3 26 27 var shared int 28 var comm chan int 29 30 func main() { 31 iterations, err := strconv.Atoi(os.Getenv("ITERATIONS")) 32 if err != nil { 33 log.Fatal(err) 34 } 35 comm = make(chan int, THREADS) 36 go op(iterations, add) 37 go op(iterations, sub) 38 go op(iterations, mult) 39 //go op(div) 40 comm <- 1 41 comm <- 2 42 comm <- 3 43 log.Println("Main thread about to sleep") 44 time.Sleep(3 * time.Second) 45 log.Println(shared) 46 } 47 48 func op(iterations int, oper func(int)) { 49 for i:=1;i<iterations;i++{ 50 val := <- comm 51 oper(val) 52 comm <- shared + i 53 time.Sleep(time.Nanosecond) 54 } 55 } 56 57 func add(n int) { 58 shared += n 59 log.Println("add ->",shared) 60 } 61 62 func sub(n int) { 63 shared -= n 64 log.Println("sub ->",shared) 65 } 66 67 func mult(n int) { 68 shared *= n 69 log.Println("mult ->",shared) 70 } 71 72 73 74