Correcting a Race condition in pipeline.go

This commit is contained in:
Mihalis Tsoukalos 2019-06-13 18:15:59 +03:00
commit 79ad734976

80
ch09/plNoRace.go Normal file
View file

@ -0,0 +1,80 @@
package main
import (
"fmt"
"math/rand"
"os"
"strconv"
"time"
)
var CLOSEA = false
var DATA = make(map[int]bool)
var signal chan struct{}
func random(min, max int) int {
return rand.Intn(max-min) + min
}
func first(min, max int, out chan<- int) {
for {
select {
case <-signal:
close(out)
return
case out <- random(min, max):
}
}
}
func second(out chan<- int, in <-chan int) {
for x := range in {
_, ok := DATA[x]
if ok {
signal <- struct{}{}
} else {
fmt.Print(x, " ")
DATA[x] = true
out <- x
}
}
fmt.Println()
close(out)
}
func third(in <-chan int) {
var sum int
sum = 0
for x2 := range in {
sum = sum + x2
}
fmt.Printf("The sum of the random numbers is %d.\n", sum)
}
func main() {
if len(os.Args) != 3 {
fmt.Println("Need two integer parameters!")
return
}
n1, _ := strconv.Atoi(os.Args[1])
n2, _ := strconv.Atoi(os.Args[2])
if n1 > n2 {
fmt.Printf("%d should be smaller than %d.\n", n1, n2)
return
}
signal = make(chan struct{})
rand.Seed(time.Now().UnixNano())
A := make(chan int)
B := make(chan int)
go first(n1, n2, A)
go second(B, A)
third(B)
}