Created
February 7, 2019 15:03
-
-
Save thapakorn613/81a4288dd7ec4b3bfdaebed2698bc682 to your computer and use it in GitHub Desktop.
homework2-590610613-parallel_proc_and_dist_sys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
func main() { | |
Barrier(5); | |
} | |
func Barrier(n int){ | |
generation := make(chan int) | |
waitSync := make(chan bool,n) | |
pulseSync := make(chan bool,n) | |
for i := 0 ; i < n ;i++ { | |
go synchronized(generation,waitSync,pulseSync,n) | |
} | |
generation <- 1 | |
<- waitSync | |
} | |
func synchronized(generation chan int ,waitSync chan bool, pulseSync chan bool,n int){ | |
pulseSync <- true | |
count := <- generation | |
fmt.Println("In put :", count) | |
if ( count == n) { | |
PulseAll(n,waitSync) | |
}else { | |
generation <- count + 1 | |
Wait(waitSync) | |
} | |
} | |
func PulseAll(n int,waitSync chan bool) { | |
for i := 0; i < n; i++ { | |
waitSync <- true | |
} | |
} | |
func Wait(waitSync chan bool) { | |
<- waitSync | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment