Deadlock - all goroutines are sleeping (even using wait groups)

PHPz
Release: 2024-02-09 13:40:08
forward
1116 people have browsed it

死锁 - 所有 goroutine 都处于睡眠状态(即使使用等待组)

php 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()

}
Copy after login

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()
Copy after login

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
    }
}
Copy after login

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
....
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!