Deadlock - all goroutines are sleeping (even using wait groups)
Feb 09, 2024 pm 01:40 PMphp editor Xiaoxin is here to introduce to you a common concurrent programming problem-deadlock. Deadlock means that all goroutines have entered the sleep state and cannot continue to execute even if mechanisms such as waiting groups are used. In this case, all goroutines are unable to move forward, causing the program to fall into an infinite waiting state. In concurrent programming, it is very important to avoid deadlock, and we need to understand its causes and solutions to ensure the normal operation of the program.
Question content
I'm learning go concurrency and I want two go routines to continue communicating with each other while passing updated values through a channel. One adds 2 and the other subtracts 1. The code and output are as follows:
What's wrong with this code?
package main import ( "fmt" "sync" "time" ) var wg sync.waitgroup func addtwo(r chan int, e chan int) { val := <-r fmt.println("at recieved: ", val) time.sleep(1 * time.second) e <- val + 2 } func subtractone(r chan int, e chan int) { val := <-r fmt.println("so recieved: ", val) time.sleep(1 * time.second) e <- val - 1 } func main() { ch := make(chan int) ch2 := make(chan int) wg.add(1) go addtwo(ch, ch2) wg.add(1) go subtractone(ch2, ch) ch <- 1 wg.wait() }
Output:
AT Recieved: 1 SO Recieved: 3 fatal error: all goroutines are asleep - deadlock! goroutine 1 [semacquire]: sync.runtime_Semacquire(0x4b2de8?) /usr/lib/go-1.18/src/runtime/sema.go:56 +0x25 sync.(*WaitGroup).Wait(0x0?) /usr/lib/go-1.18/src/sync/waitgroup.go:136 +0x52 main.main()
Then it exits.
Why doesn't the goroutine always swap values, even if I don't call wg.done() in the goroutine?
Solution
Both goroutines you start only receive one value and send one value, and then end. From there, there is only the main
goroutine, which is blocked at wg.wait()
because you never called wg.done()
.
You forgot to use an (n infinite) loop:
func addtwo(r chan int, e chan int) { for { val := <-r fmt.println("at recieved: ", val) time.sleep(1 * time.second) e <- val + 2 } } func subtractone(r chan int, e chan int) { for { val := <-r fmt.println("so recieved: ", val) time.sleep(1 * time.second) e <- val - 1 } }
With this change, your application will run forever and the output will be:
AT Recieved: 1 SO Recieved: 3 AT Recieved: 2 SO Recieved: 4 AT Recieved: 3 SO Recieved: 5 AT Recieved: 4 ....
The above is the detailed content of Deadlock - all goroutines are sleeping (even using wait groups). For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Go language pack import: What is the difference between underscore and without underscore?

How to implement short-term information transfer between pages in the Beego framework?

How to convert MySQL query result List into a custom structure slice in Go language?

How do I write mock objects and stubs for testing in Go?

How can I define custom type constraints for generics in Go?

How can I use tracing tools to understand the execution flow of my Go applications?

How to write files in Go language conveniently?
