Is there a deadlock when using two fmt.println in a go routine?

WBOY
Release: 2024-02-09 10:15:31
forward
784 people have browsed it

在 go 例程中使用两个 fmt.println 时会出现死锁吗?

In Go language, will using two fmt.Println() printing functions cause deadlock? This is a common question, let’s answer it. First, we must understand the concept of deadlock. A deadlock is a situation where two or more processes wait for each other to complete, causing the program to be unable to continue executing. In the Go language, if you use the fmt.Println() printing function in two routines at the same time, since the standard output is thread-safe, no deadlock will occur. Therefore, you can safely use multiple fmt.Println() functions in your go routine without worrying about deadlock issues.

Question content

I'm trying to learn go and I'm experimenting on the playground. I have a very simple code. I'm trying to use structs and slices together in a go routine. I'm not sure if this would be something I'd use in production, but it seems a little off, so here it goes:

func main() {
    routinemsg := make(chan []Person)
    routinemsg2 := make(chan []Person)

    // create the person records
    p1 := newPerson("john doe", 25)
    p2 := newPerson("dohn joe", 52)
    p3 := newPerson("bohn joo", 30)

    // send a slice of Person to the first routine
    go func() { routinemsg <- []Person{p1, p2} }()

    // retrieve the slice from the first routine[in the append]
    // append p3 to the slice retrieved from the first routine
    // send the new slice to the second routine
    go func() { routinemsg2 <- append(<-routinemsg, p3) }()
    
    // I am able to see the first Println but when I insert the second one I get a deadlock error
    // also, same error if I use one Println with 2 arguments.
    fmt.Println(<-routinemsg)
    fmt.Println(<-routinemsg2)
}
Copy after login

I've heard of waiting groups, but don't know about them yet! So, be nice to me :D, thank you for your time

WORKAROUND

routinemsg There is only one send operation on you, but you have 2 receive operations: one on In the started goroutine, the other one is in the main goroutine. The value sent can only be received once by a receiver.

If the started goroutine first receives from routinemsg, then a deadlock will occur: reception in main will be blocked forever.

If the main goroutine receives first, then the started goroutine will block forever (trying to receive from it), so it can never send anything on routinemsg2, so main Receiving from routinemsg2 will also block forever: deadlock again.

Remove the fmt.println(<-routinemsg) line in main(), then the final receive from routinemsg2 can (eventually) continue and Print slice phpcnp3 containing p1, p2 and phpcnc:

[{john doe 25} {dohn joe 52} {bohn joo 30}]
Copy after login

Try it on go playground.

The above is the detailed content of Is there a deadlock when using two fmt.println in a go routine?. 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!