Skip to content

Instantly share code, notes, and snippets.

@catatsuy
Created March 7, 2016 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save catatsuy/69cdec51b76705f2598c to your computer and use it in GitHub Desktop.
Save catatsuy/69cdec51b76705f2598c to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"sync"
"time"
)
var quitLock sync.RWMutex
func main() {
quit := false
timeUp := time.After(10 * time.Second)
done := make(chan bool)
intQ := make(chan int, 20)
go func() {
for i := 0; i < 1000; i++ {
intQ <- i
quitLock.RLock()
if quit {
quitLock.RUnlock()
done <- true
break
}
quitLock.RUnlock()
}
}()
go func() {
for {
fmt.Println(<-intQ)
time.Sleep(100 * time.Millisecond)
}
}()
<-timeUp
quitLock.Lock()
quit = true
quitLock.Unlock()
<-done
fmt.Println("closing")
close(intQ)
fmt.Println("closed")
go func() {
for {
fmt.Println(<-intQ)
time.Sleep(1 * time.Second)
}
}()
time.Sleep(10 * time.Second)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment