I am a newbie in Go, i wrote this code and hope it will enter deadlock status, but failed.
var mux sync.Mutex
func main() {
runtime.GOMAXPROCS(1)
for i := 0; i < 3; i++ {
go func() {
log.Println("Trying to Lock the Mux")
mux.Lock()
log.Println("The mux is Locked")
}()
}
runtime.Gosched()
}
//Output:
//2017/01/17 08:59:42 Trying to Lock the Mux
//2017/01/17 08:59:42 The mux is Locked
//2017/01/17 08:59:42 Trying to Lock the Mux
//2017/01/17 08:59:42 Trying to Lock the Mux
As you can see. this code run well and print something and then quit without any deadlock error. For I know, the first go func(){} goroutine has returned and locked the mux then quit. But the other two goroutines will be blocked because the mux has already blocked.
The function runtime.Gosched() should push the main goroutine to the only FIFO queue(runtime.GOMAXPROCS(1)) right? why it can be excute before the left two goroutine that already in the queue?
BTW,this following code will return the deadlock error as expected
var mux sync.Mutex
func main() {
runtime.GOMAXPROCS(1)
var wg sync.WaitGroup
wg.Add(3)
for i := 0; i < 3; i++ {
go func() {
defer wg.Done()
log.Println("Trying to Lock the Mux")
mux.Lock()
log.Println("The mux is Locked")
}()
}
wg.Wait()
}
Thanks!