Last active
November 28, 2020 03:12
-
-
Save prantoran/679af72fe6270ae44a08a5cd64a7aa47 to your computer and use it in GitHub Desktop.
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" | |
"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