source: doc/theses/colby_parsons_MMath/benchmarks/waituntil/go/order/order.go

Last change on this file was f945fa7, checked in by Peter A. Buhr <pabuhr@…>, 10 hours ago

fix spelling mistake in directory name

  • Property mode set to 100644
File size: 1.9 KB
Line 
1package main
2
3import (
4 "fmt"
5 "sync"
6 "time"
7 "runtime"
8)
9
10var Channels, ChannelSize int = 2, 100
11var cons_done, prod_done bool = false, false;
12var total_operations uint64 = 0
13var m sync.Mutex
14
15var prodJoin chan int = make(chan int)
16var consJoin chan int = make(chan int)
17
18func selectconsumer( chans [] chan uint64 ) {
19 var count uint64 = 0
20 for {
21 if cons_done { break }
22 select {
23 case <- chans[0]:
24 case <- chans[1]:
25 }
26 if ! prod_done { count++ }
27 }
28 m.Lock()
29 total_operations += count
30 m.Unlock()
31 consJoin <- 0
32}
33
34func consumer( chans [] chan uint64 ) {
35 var count uint64 = 0
36 for {
37 if cons_done { break }
38 <-chans[1]
39 if ! prod_done { count++ }
40 }
41 m.Lock()
42 total_operations += count
43 m.Unlock()
44 consJoin <- 0
45}
46
47func selectproducer( chans [] chan uint64 ) {
48 var count uint64 = 0
49 var checksum uint64 = 0
50 for {
51 if prod_done { break }
52 checksum = checksum ^ count
53 select {
54 case chans[0] <- count:
55 case chans[1] <- count:
56 }
57 count++
58 }
59 prodJoin <- 0
60}
61
62func producer( chans [] chan uint64 ) {
63 var count uint64 = 0
64 var checksum uint64 = 0
65 for {
66 if prod_done { break }
67 checksum = checksum ^ count
68 chans[1] <- count
69 count++
70 }
71 prodJoin <- 0
72}
73
74
75func main() {
76 runtime.GOMAXPROCS( 4 );
77
78 // fmt.Println("Processors: ",Processors," Channels: ",Channels," ProdsPerChan: ",ProdsPerChan," ConsPerChan: ",ConsPerChan," Channel Size: ",ChannelSize)
79
80 chans := make( [] chan uint64, Channels )
81 for i := range chans {
82 chans[i] = make(chan uint64, ChannelSize)
83 }
84
85
86 go selectproducer( chans )
87 go selectconsumer( chans )
88 go producer( chans )
89 go consumer( chans )
90
91
92 // wait 10 seconds
93 time.Sleep(time.Second * 10)
94 // fmt.Println("prod done\n")
95 prod_done = true
96 for j := 0; j < 2; j++ {
97 <-prodJoin
98 }
99 // fmt.Println("cons done\n")
100 cons_done = true
101 for i := range chans {
102 close(chans[i])
103 }
104
105 for j := 0; j < 2; j++{
106 <-consJoin
107 }
108 fmt.Println(total_operations)
109}
Note: See TracBrowser for help on using the repository browser.