dongmaopan5738 2017-01-10 07:21
浏览 64
已采纳

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

Here are the piece of code to test the mutual access of write and read of a struct B using atomic.Value, but I've got some error indicating invalid pointer access. So what should I do? And what is the idiomatic of doing this?

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
    }
}

I'm sorry I've missed something in the first post of the question. If I comment b := store.Load().(B) the error raises, and it is gone if i leave it visible. So why would this happen?

the error I've got is something like this:

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条回答 默认 最新

  • doulianqi3870 2017-01-12 04:57
    关注

    When you comment out b := store.Load().(B) the variable b used in the function is the global b which is of type *B and when you do store.Store(b) you get the error because you are trying to store *B type value in the store which has value of type B. That's why the error talks about inconsistent type. You can change store.Store(b) to store.Store(*b) and the code will work without error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突