BewareMyPower edited a comment on pull request #663:
URL: https://github.com/apache/pulsar-client-go/pull/663#issuecomment-963732361


   
https://github.com/apache/pulsar-client-go/blob/fe3b7c4e445b3de42974ca692574229ad9099a45/pulsar/internal/connection.go#L212
   
   And see https://pkg.go.dev/[email protected]#NewCond
   
   > NewCond returns a new Cond with Locker l.
   
   ```go
   // NewCond returns a new Cond with Locker l.
   func NewCond(l Locker) *Cond {
       return &Cond{L: l}
   }
   ```
   
   So I think `c.Lock()` is the same as `c.cond.L.Lock()`.
   
   Back to your issue, how could you ensure `c.cond.Wait()` must be called 
before `c.cond.BroadCast()` is called? And I think it's not necessary. Please 
see my code snippet.
   
   ```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 !flag {
                c.Wait()
        }
        fmt.Printf("After Wait, flag is %v\n", flag)
   }
   ```
   
   I called `time.Sleep` to make sure `c.Wait()` is called after 
`c.Broadcast()`. And the output is
   
   ```
   Before Broadcast
   After Broadcast
   Before Wait
   After Wait, flag is true
   ```


-- 
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