doomm4711 2016-05-05 06:19
浏览 28
已采纳

进行不正确的结构初始化?

While coding I encountered a problem. When I use method of inner struct in goroutine, I can't see inner state like in this code.

package main

import (
    "fmt"
    "time"
)

type Inner struct {
    Value int
}

func (c Inner) Run(value int) {
    c.Value = value
    for {
        fmt.Println(c.Value)
        time.Sleep(time.Second * 2)
    }

}

type Outer struct {
    In Inner
}

func (c Outer) Run()  {
    go c.In.Run(42)

    for {
        time.Sleep(time.Second)
        fmt.Println(c.In)
    }
}

func main()  {
    o := new(Outer)
    o.Run()
}

Program printing:

from inner:  {42}
from outer:  {0}
from outer:  {0}
from inner:  {42}
from outer:  {0}
from inner:  {42}
from outer:  {0}
from outer:  {0}

Maybe it's pointer problem, but I don't know how resolve it.

  • 写回答

2条回答 默认 最新

  • dongmopu6734 2016-05-05 06:35
    关注

    The most obvious error in your code is that Inner.Run() has a value-receiver, which means it gets a copy of the Inner type. When you modify this, you modify the copy, and the caller won't see any change on the Inner value.

    So first modify it to have a pointer-receiver:

    func (c *Inner) Run(value int) {
        // ...
    }
    

    If a method has a pointer-receiver, the address (pointer) of the value the method is called on will be passed to the method. And inside the method you will modify the pointed value, not the pointer. The pointer points to the same value that is present at the caller, so the same value is modified (and not a copy).

    This change alone may make your code work. However, the output of your program is non-deterministic because you modify a variable (field) from one goroutine, and you read this variable from another goroutine too, so you must synchronize access to this field in some way.

    One way to synchronize access is using sync.RWMutex:

    type Inner struct {
        m     *sync.RWMutex
        Value int
    }
    

    When you create your Outer value, initialize this mutex:

    o := new(Outer)
    o.In.m = &sync.RWMutex{}
    

    Or in one line:

    o := &Outer{In: Inner{m: &sync.RWMutex{}}}
    

    And in Inner.Run() lock when you access the Inner.Value field:

    func (c *Inner) Run(value int) {
        c.m.Lock()
        c.Value = value
        c.m.Unlock()
    
        for {
            c.m.RLock()
            fmt.Println(c.Value)
            c.m.RUnlock()
            time.Sleep(time.Second * 2)
        }
    }
    

    And you also have to use the lock when you access the field in Outer.Run():

    func (c Outer) Run() {
        go c.In.Run(42)
    
        for {
            time.Sleep(time.Second)
            c.In.m.RLock()
            fmt.Println(c.In)
            c.In.m.RUnlock()
        }
    }
    

    Note:

    Your example only changes Inner.Value once, in the beginning of Inner.Run. So the above code does a lot of unnecessary locks/unlocks which could be removed if the loop in Outer.Run() would wait until the value is set, and afterwards both goroutines could read the variable without locking. In general if the variable can be changed at later times too, the above presented locking/unlocking is required at each read/write.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)