Skip to content

Instantly share code, notes, and snippets.

@prantoran
Last active November 28, 2020 03:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save prantoran/679af72fe6270ae44a08a5cd64a7aa47 to your computer and use it in GitHub Desktop.
Save prantoran/679af72fe6270ae44a08a5cd64a7aa47 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