A deadlock happens when a group of goroutines are waiting for each other and none of them is able to proceed.
For example:
func main() {
ch := make(chan int)
ch <- 1
fmt.Println(<-ch)
}
But is there any possibility the deadlock happened if we don't use channel?
CodePudding user response:
In order to have a deadlock, you just need one (or more) components to be waiting in such a way that noone will proceed first.
A channel is a common way to experience a deadlock in Go, but anything that is used for synchronization can trigger it as well.
Here are just some examples of simple deadlocks:
Mutex:
package main
import "sync"
func main() {
var mu sync.Mutex
mu.Lock()
mu.Lock()
}
WaitGroup:
package main
import "sync"
func main() {
var wg sync.WaitGroup
wg.Add(1)
wg.Wait()
}