Skip to content

Instantly share code, notes, and snippets.

@thapakorn613
Created February 7, 2019 15:03
Show Gist options
  • Save thapakorn613/81a4288dd7ec4b3bfdaebed2698bc682 to your computer and use it in GitHub Desktop.
Save thapakorn613/81a4288dd7ec4b3bfdaebed2698bc682 to your computer and use it in GitHub Desktop.
homework2-590610613-parallel_proc_and_dist_sys
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