Using atomic operations, cache consistency will cause some threads to endless loop?

When I use the following test code, some threads will loop. But we added println() os.Creat () or os.Open (), this will not happen。

	var casValue = new(int64)
	*casValue = 0
	var old int64 = 0
	var new int64 = 1
	var container *list.List
	container = list.New()
	var wg sync.WaitGroup
	wg.Add(10)
	t1 := time.Now()
	for i := 0; i < 10; i++ {
		go func(i int) {
			for j := 0; j < 10000; {
				if atomic.CompareAndSwapInt64(casValue, old, new) {
					var test = "hello world,hello world,hello world"
					container.PushBack(test)
					*casValue = 0
					j++
				}
				//println("hello")、os.Create("test")、os.Open("test") will solve this problem
			}
			println("casValue=", *casValue)
			wg.Done()
		}(i)
	}
	lentcy := time.Since(t1)
	wg.Wait()
	fmt.Println(container.Len())

You have a data race, Therefore, your results are undefined.


Go: Data Race Detector


package main

import (
	"container/list"
	"fmt"
	"sync"
	"sync/atomic"
	"time"
)

func main() {

	var casValue = new(int64)
	*casValue = 0
	var old int64 = 0
	var new int64 = 1
	var container *list.List
	container = list.New()
	var wg sync.WaitGroup
	wg.Add(10)
	t1 := time.Now()
	for i := 0; i < 10; i++ {
		go func(i int) {
			for j := 0; j < 10000; {
				if atomic.CompareAndSwapInt64(casValue, old, new) {
					var test = "hello world,hello world,hello world"
					container.PushBack(test)
					*casValue = 0
					j++
				}
				//println("hello")、os.Create("test")、os.Open("test") will solve this problem
			}
			println("casValue=", *casValue)
			wg.Done()
		}(i)
	}
	wg.Wait()
	latency := time.Since(t1)
    fmt.Println(latency)
	fmt.Println(container.Len())

}

.

$ go run -race racer.go
==================
WARNING: DATA RACE
Write at 0x00c00013a010 by goroutine 8:
  sync/atomic.CompareAndSwapInt64()
      /home/peter/go/src/runtime/race_amd64.s:327 +0xb
  main.main.func1()
      /home/peter/Sync/gopath/mod/gf/racer.go:25 +0x84

Previous write at 0x00c00013a010 by goroutine 7:
  main.main.func1()
      /home/peter/Sync/gopath/mod/gf/racer.go:28 +0x312

Goroutine 8 (running) created at:
  main.main()
      /home/peter/Sync/gopath/mod/gf/racer.go:23 +0x23d

Goroutine 7 (running) created at:
  main.main()
      /home/peter/Sync/gopath/mod/gf/racer.go:23 +0x23d
==================
==================
WARNING: DATA RACE
Read at 0x00c000124158 by goroutine 9:
  container/list.(*List).PushBack()
      /home/peter/go/src/container/list/list.go:155 +0x104
  main.main.func1()
      /home/peter/Sync/gopath/mod/gf/racer.go:27 +0x93

Previous write at 0x00c000124158 by goroutine 7:
  container/list.(*List).insert()
      /home/peter/go/src/container/list/list.go:96 +0x27c
  container/list.(*List).insertValue()
      /home/peter/go/src/container/list/list.go:104 +0x113
  container/list.(*List).PushBack()
      /home/peter/go/src/container/list/list.go:155 +0xf5
  main.main.func1()
      /home/peter/Sync/gopath/mod/gf/racer.go:27 +0x93

Goroutine 9 (running) created at:
  main.main()
      /home/peter/Sync/gopath/mod/gf/racer.go:23 +0x23d

Goroutine 7 (running) created at:
  main.main()
      /home/peter/Sync/gopath/mod/gf/racer.go:23 +0x23d
==================
<SNIP>

When I cancel the println (“hello”) annotation, there is no data conflict. What’s the reason?

Go version is upgraded from 1.13.5 to 1.14.1, and the problem is solved,thank you very much!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.