如何在golang中使用原子的加载和存储

时间:2017-01-10 07:21:53

标签: 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 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)错误引发,如果我让它可见,它就消失了。那为什么会发生这种情况呢?

我得到的错误是这样的:

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

1 个答案:

答案 0 :(得分:0)

当您对b := store.Load().(B)发表评论时,该函数中使用的变量b是全局b类型*B,当您执行store.Store(b)时获取错误是因为您尝试在类型为*B的值的商店中存储B类型值。这就是错误谈论不一致类型的原因。您可以将store.Store(b)更改为store.Store(*b),代码将正常运行。