Guest User

Untitled

a guest
Mar 20th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "time"
  6. "sync"
  7. )
  8.  
  9. type Ids struct {
  10. e []int64
  11. sync.RWMutex
  12. }
  13.  
  14. func (i *Ids) Read() []int64 {
  15. i.RLock()
  16. defer i.RUnlock()
  17.  
  18. return i.e
  19. }
  20.  
  21.  
  22. func (i *Ids) Append(int int64) {
  23. i.Lock()
  24. defer i.Unlock()
  25.  
  26. i.e = append(i.e, int)
  27. }
  28.  
  29. func main() {
  30. t := &Ids{e: make([]int64, 1)}
  31.  
  32. for i := 0; i < 100; i++ {
  33. go func() {
  34. fmt.Printf("%vn", t.Read())
  35. }()
  36.  
  37. go func() {
  38. t.Append(int64(i))
  39. }()
  40. }
  41.  
  42. time.Sleep(time.Second * 10)
  43. }
  44.  
  45. ==================
  46. WARNING: DATA RACE
  47. Read at 0x00c4200a0010 by goroutine 7:
  48. main.main.func2()
  49. .../main.go:38 +0x38
  50.  
  51. Previous write at 0x00c4200a0010 by main goroutine:
  52. main.main()
  53. .../main.go:32 +0x197
  54.  
  55. Goroutine 7 (running) created at:
  56. main.main()
  57. .../main.go:37 +0x173
  58. ==================
Add Comment
Please, Sign In to add comment