title: How to correctly use sync.Cond?
body: <p>I'm having trouble figuring out how to correctly use <a href="https://golang.org/pkg/sync/#Cond" rel="noreferrer"><code>sync.Cond</code></a>. From what I can tell, a race condition exists between locking the Locker and invoking the condition's Wait method. This example adds an artificial delay between the two lines in the main goroutine to simulate the race condition:</p>
  
  <pre><code>package main
  
  import (
      "sync"
      "time"
  )
  
  func main() {
      m := sync.Mutex{}
      c := sync.NewCond(&amp;m)
      go func() {
          time.Sleep(1 * time.Second)
          c.Broadcast()
      }()
      m.Lock()
      time.Sleep(2 * time.Second)
      c.Wait()
  }
  </code></pre>
  
  <p><sup>[<a href="http://play.golang.org/p/aV9ZefLt3V" rel="noreferrer">Run on the Go Playground</a>]</sup></p>
  
  <p>This causes an immediate panic:</p>
  
  <pre>fatal error: all goroutines are asleep - deadlock!
  
  goroutine 1 [semacquire]:
  sync.runtime_Syncsemacquire(0x10330208, 0x1)
      /usr/local/go/src/runtime/sema.go:241 +0x2e0
  sync.(*Cond).Wait(0x10330200, 0x0)
      /usr/local/go/src/sync/cond.go:63 +0xe0
  main.main()
      /tmp/sandbox301865429/main.go:17 +0x1a0</pre>
  
  <p>What am I doing wrong? How do I avoid this apparent race condition? Is there a better synchronization construct I should be using?</p>
  
  <hr>
  
  <p><strong>Edit:</strong> I realize I should have better explained the problem I'm trying to solve here. I have a long-running goroutine that downloads a large file and a number of other goroutines that need access to the HTTP headers when they are available. This problem is harder than it sounds.</p>
  
  <p>I can't use channels since only one goroutine would then receive the value. And some of the other goroutines would be trying to retrieve the headers long after they are already available.</p>
  
  <p>The downloader goroutine could simply store the HTTP headers in a variable and use a mutex to safeguard access to them. However, this doesn't provide a way for the other goroutines to "wait" for them to become available.</p>
  
  <p>I had thought that both a <code>sync.Mutex</code> and <code>sync.Cond</code> together could accomplish this goal but it appears that this is not possible.</p>