Skip to content

Instantly share code, notes, and snippets.

@yifan-gu
Created December 19, 2013 21:15
Show Gist options
  • Save yifan-gu/8046382 to your computer and use it in GitHub Desktop.
Save yifan-gu/8046382 to your computer and use it in GitHub Desktop.
about synchronization in go
according to http://golang.org/ref/mem (especially the last example)
Will BenchmakTCP be guarantted to read tcpStarted = true next time?
Will server be guaranteed to Listen before client's Dial??
func BenchmarkTCP(b *testing.B) {
done := make(chan bool, 10)
if !tcpStarted {
go startServer()
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for i := 0; i < 10; i++ {
go client(done)
}
for i := 0; i < 10; i++ {
<-done
}
}
}
func startServer() {
tcpStarted = true
ln, err := net.Listen("tcp", ":7777")
if err != nil {
panic(err)
}
for {
conn, err := ln.Accept()
if err != nil {
panic(err)
}
go io.Copy(conn, conn)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment