Created
January 28, 2020 16:58
-
-
Save krishnakumar4a4/52d3ab466f95cd66de999aa145b47e74 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" | |
"sync" | |
"time" | |
"runtime" | |
) | |
func main() { | |
m := sync.Mutex{} | |
c := sync.NewCond(&m) | |
go func() { | |
c.L.Lock() | |
fmt.Println("started go routine1 - ", runtime.NumGoroutine()) | |
for !cond() { | |
fmt.Println("started wait1 - ", runtime.NumGoroutine()) | |
c.Wait() | |
fmt.Println("ended wait1 - ", runtime.NumGoroutine()) | |
} | |
fmt.Println("end go routine1 - ", runtime.NumGoroutine()) | |
c.L.Unlock() | |
}() | |
go func() { | |
fmt.Println("started go routine2 - ", runtime.NumGoroutine()) | |
c.L.Lock() | |
for !cond() { | |
fmt.Println("started wait2 - ", runtime.NumGoroutine()) | |
c.Wait() | |
fmt.Println("ended wait2 - ", runtime.NumGoroutine()) | |
} | |
c.L.Unlock() | |
fmt.Println("end go routine2 - ", runtime.NumGoroutine()) | |
}() | |
time.Sleep(time.Second * 2) | |
fmt.Println("Hello, playground - ", runtime.NumGoroutine()) | |
c.L.Lock() | |
c.Broadcast() | |
c.L.Unlock() | |
time.Sleep(time.Second * 2) | |
fmt.Println("Hello, playground2 - ", runtime.NumGoroutine()) | |
time.Sleep(time.Second * 15) | |
fmt.Println("Hello, playground3 - ", runtime.NumGoroutine()) | |
} | |
func cond() bool { | |
// time.Sleep(10 * time.Second) | |
return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment