What is deadlock? In Go's coroutine, deadlock is usually permanently blocked. If you hold my stuff, you ask me to give it to you first and then to me. If I hold your stuff, you ask you to give it to me first, otherwise I won't give it to you. We both think so, and this matter cannot be solved.
The first situation: a pipeline without cache capability, write it yourself and read it yourself
First upload the code:
func main() { ch := make(chan int, 0) ch <- 666 x := <- ch (x) }
We can see that this is a pipeline without cache capability, and then write 666 into it, and then read it in the pipeline. This will definitely lead to problems! A pipeline without cache capability, you can’t write it, no one can write it, no one can write it, no one can read it, this is a deadlock!
fatal error: all goroutines are asleep - deadlock!
The solution is very simple: create two coroutines, one coroutine writes, and one coroutine reads.
The second situation: the coroutine is late
func main() { ch := make(chan int,0) ch <- 666 go func() { <- ch }() }
We can see that after this coroutine is opened, the pipeline cannot be written because no one reads it, and the operation of writing to the pipeline will be blocked. At this time, you are confused. Didn’t you open up a coroutine to read? However, after the coroutine is opened, if it cannot be written to the pipeline, it cannot be opened.
The third situation: When reading and writing the pipeline, each other requires each other to read/write first.
If each other requires each other to read/write first and then read/write yourself, it will cause a deadlock.
func main() { chHusband := make(chan int,0) chWife := make(chan int,0) go func() { select { case <- chHusband: chWife<-888 } }() select { case <- chWife: chHusband <- 888 } }
Let’s take a look at my wife’s coroutine first. As long as I can read it, that is, my wife is rich, I will give my husband a big red envelope of 888.
Let’s look at my husband’s coroutine again. I saw it so hard. What’s wrong? My husband also said that as long as he has money, he would give his wife a big red envelope of 888.
Both of them said that they had no money, and their husbands could not send red envelopes to their wives, and their wives could not send red envelopes to their husbands. This is a deadlock!
The fourth situation: read and write locks block each other, forming an invisible dead lock
Let's take a look at the code:
func main() { var rmw09 ch := make(chan int,0) go func() { () ch <- 123 () }() go func() { () x := <- ch ("Readed",x) () }() for { () } }
If the first coroutine first grabs the write-only lock for these two coroutines, and the other coroutine cannot grab the read-only lock, then because the other coroutine is not read, the first coroutine cannot be written.
If the second coroutine grabs the read-only lock first, the other coroutine cannot grab the write-only lock, then because the other coroutine is not written, the second coroutine cannot be read.
This is the end of this article about detailed explanations of common deadlock situations in Golang concurrency operations. For more related content on Golang concurrency deadlock, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!