Skip to content

Instantly share code, notes, and snippets.

@suapapa
Last active August 29, 2015 14:27
Show Gist options
  • Save suapapa/ad4b2eb488240e27e968 to your computer and use it in GitHub Desktop.
Save suapapa/ad4b2eb488240e27e968 to your computer and use it in GitHub Desktop.
goroutine
package main
import "sync"
type atom struct {
sync.Mutex
i uint64
}
func main() {
var a atom
for {
go func() {
a.Lock()
println(a.i, "i'm in")
// println(a.i, time.Now().Format(time.RFC3339Nano))
a.i++
println(a.i, "is next turn")
a.Unlock()
select {}
}()
}
}
package main
import "time"
func main() {
c := make(chan uint64)
go func() {
c <- 0
}()
for {
go func() {
i := <-c
println(i, "i'm in")
// println(a.i, time.Now().Format(time.RFC3339Nano))
c <- i + 1
select {}
}()
}
time.Sleep(10 * time.Second)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment