In Golang, when two processes known as goroutines run concurrently and block each other's pathways, it's known as a deadlock.
Let's look at the following illustration for a better understanding:
In this illustration, we have two processes, Process 1 and Process 2, and there are two resources as variables, Variable i
and Variable j
. Both Process 1
and Process 2
acquire one of the two variables. At the same time, they both need each other's resources to perform their processes. So they begin to wait for each other to release their respective resource so they could perform their tasks. This mutual anticipation from each other to release the resource becomes a deadlock for both processes.
Let's look at an example of a deadlock in a program.
package mainfunc main() {println("Channels are in Deadlock")Channel := make(chan int)Channel2 := make(chan int)<- Channel<- Channel2}
Lines 4–5: We create two channels with the name Channel
and Channel2
.
Lines 7–8: Both channels are deadlocked with each other.
Free Resources