title: golang use channel with time out pattern to jump out of loop
body: <p>Learning from go time out pattern <a href="http://blog.golang.org/go-concurrency-patterns-timing-out-and" rel="nofollow noreferrer">go concurrency patterns</a>, I try to check a channel and to break out of a for loop</p>
  
  <pre><code>Loop: 
     for {
        //do something repeatedly very fast in the for loop
  
       //check exitMessage to see whether to break out or not
        select {
            case &lt;- exitMessage:
                 break Loop
            case &lt;- time.After(1 * time.Millisecond):
         }
     }  
  </code></pre>
  
  <p>The time-out avoids <code>select</code> gets stuck reading from a channel. The problem is that on a Windows XP machine, that delay is much longer than 1 millisecond (per <a href="https://stackoverflow.com/questions/19847533/interrupt-pattern-for-google-go-goroutine-speed-issue">time delay inaccuracy problem</a>) which slows down the for-loop significantly.</p>
  
  <p>A hacked-up solution is to get another goroutine (I know it's cheap) to listen for the <code>exitMessage</code></p>
  
  <pre><code>exitFlag := 0
  
  //another goroutine to check exitMessage
  go fun(in chan int){
      exitFlag = &lt;-in
  }(exitMessage)
  
  
  for exitFlag == 0 {
         //do something repeatedly very fast in the for loop
  
  
   }
  </code></pre>
  
  <p>Is there a better pattern to interrupt a for-loop in go?</p>