Created
March 7, 2016 15:17
-
-
Save catatsuy/69cdec51b76705f2598c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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