Skip to content

Instantly share code, notes, and snippets.

@hackintoshrao
Last active January 15, 2016 12:39
Show Gist options
  • Save hackintoshrao/f7b67729fb580c0e8d44 to your computer and use it in GitHub Desktop.
Save hackintoshrao/f7b67729fb580c0e8d44 to your computer and use it in GitHub Desktop.
the synchronization challenge
package main
import (
"fmt"
"time"
)
func main() {
bridge := make(chan int) //creating the channel
go my_func(bridge) //creating a new go routine and sharing the reference of the channel
for i := 0; i < 10; i++ {
bridge <- i //sending integer through the bridge(channel), this is blocked till its recieved in the goroutine
fmt.Println("in the main function ")
}
time.Sleep(1 * time.Second)
}
func my_func(referenceToMainBridge chan int) {
for i := 0; i < 10; i++ {
time.Sleep(1 * time.Second)
<-referenceToMainBridge // the recieved value is not used, its just disposed
//main is blocked till its recieved here
fmt.Println("In the Go routine ")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment