Skip to content

Instantly share code, notes, and snippets.

@singchia
Last active May 31, 2018 00:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save singchia/5a4b7eb344b5de6dbc63b723c8fb0473 to your computer and use it in GitHub Desktop.
Save singchia/5a4b7eb344b5de6dbc63b723c8fb0473 to your computer and use it in GitHub Desktop.
race.go
package main
import (
"fmt"
"math/rand"
"os"
"strconv"
"sync"
"time"
)
func main() {
race := make(map[int]int)
/*
var keys []int
for i := 0; i < 10000; i++ {
keys = append(keys, i)
}
*/
for j := 0; j < 5000; j++ {
race[j] = rand.Int()
}
for i := 0; i < 1; i++ {
go func() {
race2 := make(map[int]int, len(race))
for j := 5000; j < 8000; j++ {
race2[j] = rand.Int()
}
race = race2
}()
}
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func(index int) {
fd, _ := os.Create(strconv.FormatInt(time.Now().Unix(), 10) + "-" + strconv.Itoa(index))
var keys []int
for k := range race {
keys = append(keys, k)
}
for _, k := range keys {
fd.Write([]byte(fmt.Sprintf("id: %d, key: %d, value: %d\n", index, k, race[k])))
}
fd.Close()
wg.Done()
}(i)
}
wg.Wait()
/*
fd1, _ := os.Create("singchia")
for _, i := range keys {
v, ok := race[i]
if ok {
fd1.Write([]byte(fmt.Sprintf("key: %d, value: %d\n", i, v)))
} else {
fd1.Write([]byte(fmt.Sprintf("key: %d, not exists\n", i)))
}
}
fd1.Close()
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment