Skip to content

Instantly share code, notes, and snippets.

@akshaybharambe14
Forked from prantoran/synccondsimple.go
Created April 11, 2019 06:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akshaybharambe14/edac9b2505c40b5e4624c515477923b2 to your computer and use it in GitHub Desktop.
Save akshaybharambe14/edac9b2505c40b5e4624c515477923b2 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"os"
"os/signal"
"sync"
"time"
)
func listen(name string, a map[string]int, c *sync.Cond) {
c.L.Lock()
c.Wait()
fmt.Println(name, " age:", a["T"])
c.L.Unlock()
}
func broadcast(name string, a map[string]int, c *sync.Cond) {
time.Sleep(time.Second)
c.L.Lock()
a["T"] = 25
c.Broadcast()
c.L.Unlock()
}
func main() {
var age = make(map[string]int)
m := sync.Mutex{}
cond := sync.NewCond(&m)
// listener 1
go listen("lis1", age, cond)
// listener 2
go listen("lis2", age, cond)
// broadcast
go broadcast("b1", age, cond)
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
<-ch
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment