1 | package main |
---|
2 | |
---|
3 | import ( |
---|
4 | "fmt" |
---|
5 | "sync" |
---|
6 | "time" |
---|
7 | "runtime" |
---|
8 | "os" |
---|
9 | "strconv" |
---|
10 | ) |
---|
11 | |
---|
12 | var Processors, Channels, Prods, Cons, ChannelSize int = 2, 2, 1, 1, 10 |
---|
13 | var cons_done, prod_done bool = false, false; |
---|
14 | var total_operations uint64 = 0 |
---|
15 | var m sync.Mutex |
---|
16 | |
---|
17 | var prodJoin chan int = make(chan int) |
---|
18 | var consJoin chan int = make(chan int) |
---|
19 | |
---|
20 | func consumer( chans [] chan uint64 ) { |
---|
21 | var count uint64 = 0 |
---|
22 | for { |
---|
23 | if cons_done { break } |
---|
24 | |
---|
25 | select { |
---|
26 | case <- chans[0]: |
---|
27 | if ! prod_done { count++ } |
---|
28 | case <- chans[1]: |
---|
29 | if ! prod_done { count++ } |
---|
30 | default: |
---|
31 | } |
---|
32 | } |
---|
33 | m.Lock() |
---|
34 | total_operations += count |
---|
35 | m.Unlock() |
---|
36 | consJoin <- 0 |
---|
37 | } |
---|
38 | |
---|
39 | func producer( chans [] chan uint64 ) { |
---|
40 | var count uint64 = 0 |
---|
41 | for { |
---|
42 | if prod_done { break } |
---|
43 | select { |
---|
44 | case chans[0] <- count: |
---|
45 | case chans[1] <- count: |
---|
46 | default: |
---|
47 | } |
---|
48 | count++ |
---|
49 | } |
---|
50 | prodJoin <- 0 |
---|
51 | } |
---|
52 | |
---|
53 | func usage() { |
---|
54 | fmt.Printf( "Usage: %v " + |
---|
55 | "[ processors (> 0) | 'd' (default %v) ] " + |
---|
56 | "[ ChannelSize (> 0) | 'd' (default %v) ]\n", |
---|
57 | os.Args[0], Processors, ChannelSize ); |
---|
58 | os.Exit( 1 ); |
---|
59 | } |
---|
60 | |
---|
61 | func main() { |
---|
62 | switch len( os.Args ) { |
---|
63 | case 3: |
---|
64 | if os.Args[2] != "d" { // default ? |
---|
65 | ChannelSize, _ = strconv.Atoi( os.Args[2] ) |
---|
66 | if ChannelSize < 0 { usage(); } |
---|
67 | } // if |
---|
68 | fallthrough |
---|
69 | case 2: |
---|
70 | if os.Args[1] != "d" { // default ? |
---|
71 | Processors, _ = strconv.Atoi( os.Args[1] ) |
---|
72 | if Processors < 1 { usage(); } |
---|
73 | } // if |
---|
74 | case 1: // use defaults |
---|
75 | default: |
---|
76 | usage(); |
---|
77 | } // switch |
---|
78 | runtime.GOMAXPROCS( Processors ); |
---|
79 | Prods = Processors /2 |
---|
80 | Cons = Processors / 2 |
---|
81 | |
---|
82 | // fmt.Println("Processors: ",Processors," Channels: ",Channels," ProdsPerChan: ",ProdsPerChan," ConsPerChan: ",ConsPerChan," Channel Size: ",ChannelSize) |
---|
83 | |
---|
84 | chans := make( [] chan uint64, Channels ) |
---|
85 | for i := range chans { |
---|
86 | chans[i] = make(chan uint64, ChannelSize) |
---|
87 | } |
---|
88 | for j := 0; j < Prods; j++ { |
---|
89 | go producer( chans ) |
---|
90 | } |
---|
91 | |
---|
92 | for j := 0; j < Cons; j++ { |
---|
93 | go consumer( chans ) |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | // wait 10 seconds |
---|
98 | time.Sleep(time.Second * 10) |
---|
99 | // fmt.Println("prod done\n") |
---|
100 | prod_done = true |
---|
101 | for j := 0; j < Prods; j++ { |
---|
102 | <-prodJoin |
---|
103 | } |
---|
104 | // fmt.Println("cons done\n") |
---|
105 | cons_done = true |
---|
106 | for i := range chans { |
---|
107 | close(chans[i]) |
---|
108 | } |
---|
109 | |
---|
110 | for j := 0; j < Cons; j++{ |
---|
111 | <-consJoin |
---|
112 | } |
---|
113 | |
---|
114 | fmt.Println(total_operations) |
---|
115 | } |
---|