Created
February 18, 2016 19:51
-
-
Save yaegaki/da4544c51dedef5574be to your computer and use it in GitHub Desktop.
forをchanを使って途中で止める(スレッドセーフ)
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 ( | |
"log" | |
"time" | |
) | |
func temp(quitChan chan bool) { | |
for i := 0; i <= 1000000; i++ { | |
select { | |
case <-quitChan: | |
return | |
default: | |
log.Println(i) | |
} | |
time.Sleep(10 * time.Millisecond) | |
} | |
} | |
func main() { | |
c := make(chan bool) | |
go temp(c) | |
time.Sleep(2 * time.Second) | |
close(c) | |
time.Sleep(2 * time.Second) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment