Created
December 19, 2013 21:15
-
-
Save yifan-gu/8046382 to your computer and use it in GitHub Desktop.
about synchronization in go
This file contains hidden or 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
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