bschofield commented on pull request #663:
URL: https://github.com/apache/pulsar-client-go/pull/663#issuecomment-963949062


   @BewareMyPower: I tried this modification of your code, which I think is 
exactly the one you suggested:
   
   ```go
   package main
   
   import (
        "fmt"
        "sync"
        "time"
   )
   
   func main() {
        m := sync.Mutex{}
        c := sync.NewCond(&m)
        flag := false
   
        defer m.Unlock()
        m.Lock()
   
        go func() {
                // No locks here
                fmt.Println("Before Broadcast")
                flag = true
                c.Broadcast()
                fmt.Println("After Broadcast")
        }()
   
        // Sleep for 2 seconds to make sure c.Broadcast() is called before 
c.Wait()
        time.Sleep(time.Duration(2) * time.Second)
        fmt.Println("Before Wait")
        for {
                c.Wait()
                if flag {
                        break
                }
        }
        fmt.Printf("After Wait, flag is %v\n", flag)
   }
   ```
   
   When I run that, this happens:
   ```
   Before Broadcast
   After Broadcast
   Before Wait
   fatal error: all goroutines are asleep - deadlock!
   
   goroutine 1 [sync.Cond.Wait]:
   sync.runtime_notifyListWait(0xc0000b8050, 0x0)
        /home/ben/sdk/go1.17.1/src/runtime/sema.go:513 +0x13d
   sync.(*Cond).Wait(0x4b4360)
        /home/ben/sdk/go1.17.1/src/sync/cond.go:56 +0x8c
   main.main()
        /home/ben/tmp/foo.go:29 +0x191
   ```
   So, I think that test case (which you very kindly provided) confirms that we 
agree that it really is necessary to call `cond.Broadcast()`  after 
`cond.Wait()`?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to