How to correctly use sync.Cond?

前端 未结 8 919
忘了有多久
忘了有多久 2021-02-01 16:11

I\'m having trouble figuring out how to correctly use sync.Cond. From what I can tell, a race condition exists between locking the Locker and invoking the condition\'s Wait meth

8条回答
  •  我在风中等你
    2021-02-01 16:52

    Yes you can use one channel to pass Header to multiple Go routines.

    headerChan := make(chan http.Header)
    
    go func() { // This routine can be started many times
        header := <-headerChan  // Wait for header
        // Do things with the header
    }()
    
    // Feed the header to all waiting go routines
    for more := true; more; {
        select {
        case headerChan <- r.Header:
        default: more = false
        }
    }
    

提交回复
热议问题