Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "time"
- "sync"
- )
- type Ids struct {
- e []int64
- sync.RWMutex
- }
- func (i *Ids) Read() []int64 {
- i.RLock()
- defer i.RUnlock()
- return i.e
- }
- func (i *Ids) Append(int int64) {
- i.Lock()
- defer i.Unlock()
- i.e = append(i.e, int)
- }
- func main() {
- t := &Ids{e: make([]int64, 1)}
- for i := 0; i < 100; i++ {
- go func() {
- fmt.Printf("%vn", t.Read())
- }()
- go func() {
- t.Append(int64(i))
- }()
- }
- time.Sleep(time.Second * 10)
- }
- ==================
- WARNING: DATA RACE
- Read at 0x00c4200a0010 by goroutine 7:
- main.main.func2()
- .../main.go:38 +0x38
- Previous write at 0x00c4200a0010 by main goroutine:
- main.main()
- .../main.go:32 +0x197
- Goroutine 7 (running) created at:
- main.main()
- .../main.go:37 +0x173
- ==================
Add Comment
Please, Sign In to add comment