Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 戈朗如何使用原子弹的装载和储存_Go_Atomic - Fatal编程技术网

Go 戈朗如何使用原子弹的装载和储存

Go 戈朗如何使用原子弹的装载和储存,go,atomic,Go,Atomic,下面是一段代码,它使用atomic.Value测试结构B的写入和读取的相互访问,但是我遇到了一些错误,表明指针访问无效。那我该怎么办?这样做的惯用法是什么 type A struct { numMap map[string]int } type B struct { numMap map[string]*A } var store atomic.Value var chanA chan bool = make(chan bool, 100) var chanB chan b

下面是一段代码,它使用atomic.Value测试结构B的写入和读取的相互访问,但是我遇到了一些错误,表明指针访问无效。那我该怎么办?这样做的惯用法是什么

type A struct {
    numMap map[string]int
}

type B struct {
    numMap map[string]*A
}

var store atomic.Value

var chanA chan bool = make(chan bool, 100)
var chanB chan bool = make(chan bool, 100)

var b *B = &B{}

func fetchB() {
    for i := 0; i < 10000; i++ {
        c := store.Load().(B)
        for k, v := range c.numMap {
            fmt.Println(k, v)
            for k2, v2 := range v.numMap {
                fmt.Println(k2, v2)
            }
        }
    }
    chanA <- true
}

func writeB() {
    for i := 0; i < 10000; i++ {
        //b := store.Load().(B)
        a := new(A)
        a.numMap = make(map[string]int)
        a.numMap["AMap"] = i
        b.numMap = make(map[string]*A)
        b.numMap["str"] = a
        b.numMap["strA"] = a
        delete(b.numMap, "str")
        delete(b.numMap["strA"].numMap, "AMap")
        store.Store(b)
    }
    chanB <- true
}
func main() {
    store.Store(*b)
    for i := 0; i < 100; i++ {
        go writeB()
        go fetchB()
    }
    for i := 0; i < 100; i++ {
        <-chanA
        <-chanB
    }
}

当您注释掉
b:=store.Load()(b)
函数中使用的变量
b
是类型为
*b
的全局
b
,当您执行
store.store(b)
时,您会收到错误,因为您试图将
*b
类型值存储在类型为
b
的存储中。这就是为什么这个错误谈论不一致的类型。您可以将
store.store(b)
更改为
store.store(*b)
,代码将正常工作。

您使用的是哪个Go版本?我使用的是Go版本go1.5.3 linux/AMD64您可以粘贴运行程序时得到的确切错误吗?该程序可以同时访问地图。使用运行应用程序。若要扩展上一条注释,地图不是goroutine安全的。如果您正在对映射进行并发访问,并且一个或多个goroutine可能正在更改它,则需要使用互斥锁(或等效物)来保护它。
panic: sync/atomic: store of inconsistently typed value into Value

goroutine 5 [running]: sync/atomic.(*Value).Store(0x597310, 0x4b7f40, 0x597198)
    /usr/local/go/src/sync/atomic/value.go:76 +0x1e9 main.writeB()
    /home/bzhang/devCode/common/src/go/src/audience_service/test.go:47 +0x2cc created by main.main
    /home/bzhang/devCode/common/src/go/src/audience_service/test.go:54 +0x71

goroutine 1 [chan receive]: main.main()
    /home/bzhang/devCode/common/src/go/src/audience_service/test.go:59 +0xf3

goroutine 6 [runnable]: main.fetchB()
    /home/bzhang/devCode/common/src/go/src/audience_service/test.go:23 created by main.main
    /home/bzhang/devCode/common/src/go/src/audience_service/test.go:55 +0x89

goroutine 7 [runnable]: main.writeB()
    /home/bzhang/devCode/common/src/go/src/audience_service/test.go:36 created by main.main
    /home/bzhang/devCode/common/src/go/src/audience_service/test.go:54 +0x71

goroutine 8 [runnable]: main.fetchB()
    /home/bzhang/devCode/common/src/go/src/audience_service/test.go:23 created by main.main
    /home/bzhang/devCode/common/src/go/src/audience_service/test.go:55 +0x89

goroutine 9 [runnable]: main.writeB()
    /home/bzhang/devCode/common/src/go/src/audience_service/test.go:36 created by main.main
    /home/bzhang/devCode/common/src/go/src/audience_service/test.go:54 +0x7